/// <summary> /// Converts a neutral grey color to a hued color with the same level of "grey". /// </summary> public static Color GreyscaleToColor(Color greyscale, Color pureColor) { if (ColorIsPartiallyClear(greyscale)) { greyscale = ConvertPartiallyClearToGrey(greyscale); } if (ColorIsBlack(greyscale)) { return(greyscale); } if (ColorIsWhite(greyscale)) { return(pureColor); } HSV greyscaleHSV = ConvertColors.ToHSV(greyscale); HSV pureColorHSV = ConvertColors.ToHSV(pureColor); Range fullRange = new Range(MAX_VALUE_AS_BLACK, 1); Range newRange = new Range(MAX_VALUE_AS_BLACK, pureColorHSV.Value); float adjustedValue = (float)Range.ConvertValue(fullRange, newRange, greyscaleHSV.Value); float adjustedSaturation = greyscaleHSV.Value * pureColorHSV.Saturation; HSV adjustedHSV = new HSV(pureColorHSV.Hue, adjustedSaturation, adjustedValue); Color adjustedColor = ConvertColors.ToColor(adjustedHSV); return(adjustedColor); }
public HuePanel(Color?startColor = null) { this.Height = TRACKBAR_HEIGHT + 50; this.Width = UNIT * 2; this.BorderStyle = BorderStyle.Fixed3D; trackBar = new TrackBar(); trackBar.Location = new Point(0, 0); trackBar.Size = new Size(this.Width, TRACKBAR_HEIGHT); trackBar.Minimum = 0; trackBar.Maximum = 359; trackBar.ValueChanged += new EventHandler(TrackBar_OnValueChanged); this.Controls.Add(trackBar); int swatchWidth = this.Width / UNIT; int swatchY = trackBar.Location.Y + trackBar.Height; for (int hue = 0; hue < 360; hue++) { Panel colorPanel = new Panel(); colorPanel.Location = new Point(hue * swatchWidth, swatchY); colorPanel.Size = new Size(swatchWidth, this.Height); colorPanel.BackColor = ConvertColors.ToColor(new HSV(hue, 1, 1)); colorPanel.Click += new EventHandler(Color_OnClick); this.Controls.Add(colorPanel); } if (startColor.HasValue) { Hue = (int)ConvertColors.ToHSV(startColor.Value).Hue; } }
public void ColorFromHSV_ColorLibrary() { foreach (ColorLibrary.Name name in ColorLibrary.Library.Keys) { //arrange TestColor testColor = ColorLibrary.Library[name]; //act Color result = ConvertColors.ToColor(testColor.HSV); //assert Assert.AreEqual(testColor.Color.R, result.R); Assert.AreEqual(testColor.Color.G, result.G); Assert.AreEqual(testColor.Color.B, result.B); } }
protected override void OnPaint(PaintEventArgs e) { graphicsBitmap = new Bitmap(this.Width, this.Height); using (Graphics gBitmap = Graphics.FromImage(graphicsBitmap)) { for (int saturation = 0; saturation <= 100; saturation++) { for (int value = 0; value <= 100; value++) { int reverseValue = 100 - value; Color color = ConvertColors.ToColor(new HSV(hue, saturation / 100f, reverseValue / 100f)); SolidBrush brush = new SolidBrush(color); gBitmap.FillRectangle(brush, new Rectangle(saturation * swatchWidth, value * swatchWidth, swatchWidth, swatchWidth)); } } } e.Graphics.DrawImage(graphicsBitmap, 0, 0); }
/// <summary> /// Converts a hued color to its nuetral grey equivalent, based on the pure form of the hue. /// </summary> public static Color ColorToGreyscale(Color color, Color pureColor) { if (ColorIsGreyscale(color)) { return(color); } if (color == pureColor) { return(Color.White); } HSV colorHSV = ConvertColors.ToHSV(color); HSV pureColorHSV = ConvertColors.ToHSV(pureColor); Range fullRange = new Range(MAX_VALUE_AS_BLACK, 1); Range newRange = new Range(MAX_VALUE_AS_BLACK, pureColorHSV.Value); float adjustedValue = (float)Range.ConvertValue(newRange, fullRange, colorHSV.Value); HSV adjustedHSV = new HSV(0, 0, adjustedValue); Color adjustedColor = ConvertColors.ToColor(adjustedHSV); return(adjustedColor); }
private void UpdateSelectedColor() { Color = ConvertColors.ToColor(new HSV(huePanel.Hue, saturationValuePanel.Saturation / 100f, saturationValuePanel.Value / 100f)); selectedColorPanel.BackColor = Color; colorDataPanel.Color = Color; }