Пример #1
0
		/// <summary>
		/// Builds a <see cref="HSL"/> structure from the specified <see cref="Color"/> structure.
		/// </summary>
		public static HSL RGB2HSL(Color col)
		{
			float r = (float)col.R / 255f, g = (float)col.G / 255f, b = (float)col.B / 255f;
			float min = Math.Min(Math.Min(r, g), b);
			float max = Math.Max(Math.Max(r, g), b);
			float delta = max - min;

			HSL ret = new HSL(0, 0, 0);
			ret.l = (int)(50f * (max + min));

			if (delta == 0f)
			{
				ret.h = 0;
				ret.s = 0;
			}
			else
			{
				if (ret.l < 50)
					ret.s = (int)(100f * delta / (max + min));
				else
					ret.s = (int)(100f * delta / (2f - max - min));

				float del_R = (((max - r) / 6f) + (delta / 2f)) / delta;
				float del_G = (((max - g) / 6f) + (delta / 2f)) / delta;
				float del_B = (((max - b) / 6f) + (delta / 2f)) / delta;

				if (r == max)
					ret.h = (int)(360f * (del_B - del_G));
				else if (g == max)
					ret.h = (int)(360f * ((1f / 3f) + del_R - del_B));
				else if (b == max)
					ret.h = (int)(360f * ((2f / 3f) + del_G - del_R));

				if (ret.h < 0)
					ret.h += 360;
				if (ret.h > 360)
					ret.h -= 360;
			}
			return ret;
		}
Пример #2
0
		/// <summary>
		/// Builds a <see cref="Color"/> structure from the specified <see cref="HSL"/> structure.
		/// </summary>
		public static Color Hue2RGB(HSL hue)
		{
			float r, g, b, var_2, var_1;
			if (hue.s == 0)
			{
				r = g = b = (float)hue.l * 2.55f;
			}
			else
			{
				if (hue.l < 50)
					var_2 = (float)hue.l * (float)(100 + hue.s) / 10000f;
				else
					var_2 = ((float)(hue.l + hue.s) - (float)(hue.s * hue.l) / 100f) / 100f;

				var_1 = (float)hue.l / 50f - var_2;

				r = 255f * Hue_2_RGB(var_1, var_2, hue.h + 120);
				g = 255f * Hue_2_RGB(var_1, var_2, hue.h);
				b = 255f * Hue_2_RGB(var_1, var_2, hue.h - 120);
			}
			return Color.FromArgb((int)r, (int)g, (int)b);
		}
		private PointF FindColor(HSL rt)
		{
			return new PointF(1f - ((float)rt.l / 100f),
				(float)rt.h / 360f);
		}
		private void InitBrush()
		{
			if (_lbrsv == null)
			{
				_lbrsv = new LinearGradientBrush
					(new Point(0, 0), new Point(1, 0), Color.Black, Color.White);
				float dy = 0f, incy = 360f / (float)(num - 1);
				float[] pos = new float[num];
				Color[] col = new Color[num];
				HSL hue = new HSL(0, 50, 100);
				for (int i = 0; i < num; i++, dy += incy)
				{
					hue.h = (int)dy;
					pos[i] = dy / 360f;
					col[i] = HSL.Hue2RGB(hue);
				}
				ColorBlend ret = new ColorBlend();
				//pos[pos.Length-1]=1f;
				ret.Colors = col;
				ret.Positions = pos;
				_lbrsv.InterpolationColors = ret;
			}
			if (_lbrsh == null)
			{
				_lbrsh = new LinearGradientBrush
					(new Point(0, 0), new Point(1, 0), Color.Black, Color.White);
				ColorBlend ret = new ColorBlend();
				ret.Positions = new float[] { 0f, 0.5f, 1f };
				ret.Colors = new Color[] { Color.White, Color.FromArgb(0, 128, 128, 128), Color.Black };
				_lbrsh.InterpolationColors = ret;
			}
			float w = Math.Max(this.Width, 3),
				h = Math.Max(this.Height, 3);
			_lbrsv.Transform = new Matrix(0f, h, w, 0f, 0f, 0f);
			_lbrsh.Transform = new Matrix(w, 0f, 0f, h, 0f, 0f);
			_transfrm = new Matrix(w - 1f, 0f, 0f, h - 1f, 0f, 0f);
		}
		private PointF FindColor(HSL rt)
		{
			float dist = (float)(100 - Math.Max(50, rt.l)) / 50f;
			return new PointF(dist * (float)Math.Sin(Math.PI * (double)rt.h / 180.0),
				dist * (float)Math.Cos(Math.PI * (double)rt.h / 180.0));
		}