// Y = Brightness 100 -> 0 protected override void DrawBrightness() { using (var graphics = Graphics.FromImage(bmp)) { var start = new Hsb(SelectedColor.Hsb.Hue, SelectedColor.Hsb.Saturation, 1.0, SelectedColor.Rgba.Alpha); var end = new Hsb(SelectedColor.Hsb.Hue, SelectedColor.Hsb.Saturation, 0.0, SelectedColor.Rgba.Alpha); using (var brush = new LinearGradientBrush(new Rectangle(0, 0, clientWidth, clientHeight), start, end, LinearGradientMode.Vertical)) { graphics.FillRectangle(brush, new Rectangle(0, 0, clientWidth, clientHeight)); } } }
protected override void DrawHue() { using (var graphics = Graphics.FromImage(bmp)) { var color = new Hsb(0.0, 1.0, 1.0, SelectedColor.Rgba.Alpha); for (var y = 0; y < clientHeight; y++) { color.Hue = 1.0 - ((double)y / (clientHeight - 1)); using (var pen = new Pen(color)) { graphics.DrawLine(pen, 0, y, clientWidth, y); } } } }
public static Color HsbToColor(Hsb hsb) { int mid; var max = (int)Math.Round(hsb.Brightness * 255); var min = (int)Math.Round((1.0 - hsb.Saturation) * (hsb.Brightness / 1.0) * 255); var q = (double)(max - min) / 255; if (hsb.Hue >= 0 && hsb.Hue <= (double)1 / 6) { mid = (int)Math.Round((hsb.Hue - 0) * q * 1530 + min); return(Color.FromArgb(hsb.Alpha, max, mid, min)); } if (hsb.Hue <= (double)1 / 3) { return(Color.FromArgb(hsb.Alpha, (int)Math.Round(-((hsb.Hue - (double)1 / 6) * q) * 1530 + max), max, min)); } if (hsb.Hue <= 0.5) { mid = (int)Math.Round((hsb.Hue - (double)1 / 3) * q * 1530 + min); return(Color.FromArgb(hsb.Alpha, min, max, mid)); } if (hsb.Hue <= (double)2 / 3) { mid = (int)Math.Round(-((hsb.Hue - 0.5) * q) * 1530 + max); return(Color.FromArgb(hsb.Alpha, min, mid, max)); } if (hsb.Hue <= (double)5 / 6) { mid = (int)Math.Round((hsb.Hue - (double)2 / 3) * q * 1530 + min); return(Color.FromArgb(hsb.Alpha, mid, min, max)); } if (hsb.Hue <= 1.0) { mid = (int)Math.Round(-((hsb.Hue - (double)5 / 6) * q) * 1530 + max); return(Color.FromArgb(hsb.Alpha, max, min, mid)); } return(Color.FromArgb(hsb.Alpha, 0, 0, 0)); }
/// <summary> /// </summary> protected override void DrawBrightness() { using (var g = Graphics.FromImage(bmp)) { var start = new Hsb(0.0, 1.0, SelectedColor.Hsb.Brightness, SelectedColor.Rgba.Alpha); var end = new Hsb(0.0, 0.0, SelectedColor.Hsb.Brightness, SelectedColor.Rgba.Alpha); for (var x = 0; x < clientWidth; x++) { start.Hue = end.Hue = (double)x / (clientHeight - 1); using (var brush = new LinearGradientBrush(new Rectangle(0, 0, 1, clientHeight), start, end, LinearGradientMode.Vertical)) { g.FillRectangle(brush, new Rectangle(x, 0, 1, clientHeight)); } } } }
/// <summary> /// </summary> protected override void DrawHue() { using (var graphics = Graphics.FromImage(bmp)) { var start = new Hsb(SelectedColor.Hsb.Hue, 0.0, 0.0, SelectedColor.Rgba.Alpha); var end = new Hsb(SelectedColor.Hsb.Hue, 1.0, 0.0, SelectedColor.Rgba.Alpha); for (var y = 0; y < clientHeight; y++) { start.Brightness = end.Brightness = 1.0 - (double)y / (clientHeight - 1); using (var brush = new LinearGradientBrush(new Rectangle(0, 0, clientWidth, 1), start, end, LinearGradientMode.Horizontal)) { graphics.FillRectangle(brush, new Rectangle(0, y, clientWidth, 1)); } } } }
/// <summary> /// </summary> public void RgbaUpdate() { Hsb = Rgba; Cmyk = Rgba; }
/// <summary> /// </summary> /// <param name="color"></param> public MyColor(Color color) { Rgba = color; Hsb = color; Cmyk = color; }
/// <summary> /// </summary> public void CmykUpdate() { Rgba = Cmyk; Hsb = Cmyk; }
public bool Equals(Hsb other) { return(_hue.Equals(other._hue) && _saturation.Equals(other._saturation) && _brightness.Equals(other._brightness) && _alpha == other._alpha); }
public static Hsb ColorToHsb(Color color) { var hsb = new Hsb(); int max, min; if (color.R > color.G) { max = color.R; min = color.G; } else { max = color.G; min = color.R; } if (color.B > max) { max = color.B; } else if (color.B < min) { min = color.B; } var diff = max - min; hsb.Brightness = (double)max / 255; if (max == 0) { hsb.Saturation = 0; } else { hsb.Saturation = (double)diff / max; } double q; if (diff == 0) { q = 0; } else { q = (double)60 / diff; } if (max == color.R) { if (color.G < color.B) { hsb.Hue = (360 + q * (color.G - color.B)) / 360; } else { hsb.Hue = q * (color.G - color.B) / 360; } } else if (max == color.G) { hsb.Hue = (120 + q * (color.B - color.R)) / 360; } else if (max == color.B) { hsb.Hue = (240 + q * (color.R - color.G)) / 360; } else { hsb.Hue = 0.0; } hsb.Alpha = color.A; return(hsb); }