public frm_fillcolor() { InitializeComponent(); PropertyInfo aProp = typeof(Control).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance); aProp.SetValue(pnl_grad, true, null); bitmap = new Bitmap(pnl_grad.Width, 1); pnl_grad.BackgroundImage = bitmap; //color_from.SelectedHSV = Structs Structs.HSV color = new Structs.HSV(); color.h = 0; color.s = 1; color.v = 1; color_from.SelectedHSV = color; color = new Structs.HSV(); color.h = 1; color.s = 1; color.v = 1; color_to.SelectedHSV = color; color_from.OnColorChanged += delegate { UpdateGradient(color_from.SelectedHSV, color_to.SelectedHSV); }; color_to.OnColorChanged += delegate { UpdateGradient(color_from.SelectedHSV, color_to.SelectedHSV); }; UpdateGradient(color_from.SelectedHSV, color_to.SelectedHSV); }
void UpdateHSVToRGBView() { Structs.HSV hsv = new Structs.HSV(); hsv.h = (tb_h.Value / 255d); hsv.s = tb_s.Value / 255d; hsv.v = tb_v.Value / 255d; Structs.RGB rgb = ColorToolkit.hsv2rgb(hsv); Color c = ColorToolkit.rgb2color(rgb); if (!tb_r.Focused) { tb_r.Value = c.R; } if (!tb_g.Focused) { tb_g.Value = c.G; } if (!tb_b.Focused) { tb_b.Value = c.B; } UpdateTextBox(); pnl_color_view.BackColor = c; }
void UpdateGradient(Structs.HSV start, Structs.HSV end) { using (Graphics g = Graphics.FromImage(bitmap)){ using (SolidBrush brush = new SolidBrush(Color.Black)){ double h_step = (end.h - start.h) / (double)pnl_grad.Width; double s_step = (end.s - start.s) / (double)pnl_grad.Width; double v_step = (end.v - start.v) / (double)pnl_grad.Width; Structs.HSV newhsv = new Structs.HSV(); newhsv.h = start.h; newhsv.s = start.s; newhsv.v = start.v; for (int x = 0; x < bitmap.Width; x++) { brush.Color = ColorToolkit.rgb2color(ColorToolkit.hsv2rgb(newhsv)); g.FillRectangle(brush, x, 0, 1, 1); newhsv.h += h_step; newhsv.s += s_step; newhsv.v += v_step; if (newhsv.h < 0) { newhsv.h = 0; } if (newhsv.h > 1) { newhsv.h = 1; } if (newhsv.s < 0) { newhsv.s = 0; } if (newhsv.s > 1) { newhsv.s = 1; } if (newhsv.v < 0) { newhsv.v = 0; } if (newhsv.v > 1) { newhsv.v = 1; } } } } pnl_grad.Refresh(); }
public static Structs.HSV rgb2hsv(Structs.RGB rgb) { Structs.HSV hsv = new Structs.HSV(); double min, max, delta; min = rgb.r < rgb.g ? rgb.r : rgb.g; min = min < rgb.b ? min : rgb.b; max = rgb.r > rgb.g ? rgb.r : rgb.g; max = max > rgb.b ? max : rgb.b; hsv.v = max; delta = max - min; if (delta < 0.000001) { hsv.s = 0; hsv.h = 0; return(hsv); } if (max > 0.0) { hsv.s = (delta / max); } else { hsv.s = 0.0; hsv.h = 0.0; return(hsv); } if (rgb.r >= max) { hsv.h = (rgb.g - rgb.b) / delta; } else if (rgb.g >= max) { hsv.h = 2.0 + (rgb.b - rgb.r) / delta; } else { hsv.h = 4.0 + (rgb.r - rgb.g) / delta; } hsv.h /= 6d; if (hsv.h < 0.0) { hsv.h += 1d; } return(hsv); }
void UpdateRGBToHSVView() { Structs.RGB rgb = ColorToolkit.color2rgb(Color.FromArgb(tb_r.Value, tb_g.Value, tb_b.Value)); Structs.HSV hsv = ColorToolkit.rgb2hsv(rgb); if (!tb_h.Focused) { tb_h.Value = (int)((hsv.h) * 255d); } if (!tb_s.Focused) { tb_s.Value = (int)((hsv.s) * 255d); } if (!tb_v.Focused) { tb_v.Value = (int)((hsv.v) * 255d); } Color c = ColorToolkit.rgb2color(ColorToolkit.hsv2rgb(hsv)); UpdateTextBox(); pnl_color_view.BackColor = Color.FromArgb(tb_r.Value, tb_g.Value, tb_b.Value); }
public static Structs.RGB hsv2rgb(Structs.HSV hsv) { double hh, p, q, t, ff; long i; Structs.RGB rgb = new Structs.RGB(); if (hsv.s <= 0.0) { rgb.r = hsv.v; rgb.g = hsv.v; rgb.b = hsv.v; return(rgb); } hh = hsv.h; if (hh >= 1d) { hh = 1.0 - hh; } hh *= 6d; i = (long)(hh); ff = hh - i; p = hsv.v * (1.0 - hsv.s); q = hsv.v * (1.0 - (hsv.s * ff)); t = hsv.v * (1.0 - (hsv.s * (1.0 - ff))); switch (i) { case 0: rgb.r = hsv.v; rgb.g = t; rgb.b = p; break; case 1: rgb.r = q; rgb.g = hsv.v; rgb.b = p; break; case 2: rgb.r = p; rgb.g = hsv.v; rgb.b = t; break; case 3: rgb.r = p; rgb.g = q; rgb.b = hsv.v; break; case 4: rgb.r = t; rgb.g = p; rgb.b = hsv.v; break; default: rgb.r = hsv.v; rgb.g = p; rgb.b = q; break; } return(rgb); }