コード例 #1
0
        /// <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);
        }
コード例 #2
0
        private void Color_OnClick(object sender, EventArgs e)
        {
            Panel panel = sender as Panel;
            HSV   hsv   = ConvertColors.ToHSV(panel.BackColor);

            Hue = (int)hsv.Hue;
        }
コード例 #3
0
        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;
            }
        }
コード例 #4
0
        private void Gradient_OnClick(object sender, EventArgs e)
        {
            Point clickPoint = this.PointToClient(new Point(MousePosition.X, MousePosition.Y));
            Color color      = graphicsBitmap.GetPixel(clickPoint.X, clickPoint.Y);
            HSV   hsv        = ConvertColors.ToHSV(color);

            saturation = (int)(100 * hsv.Saturation);
            _value     = (int)(100 * hsv.Value);
            if (ColorChanged != null)
            {
                ColorChanged(this, new EventArgs());
            }
        }
コード例 #5
0
 public void HSVFromColor_ColorLibrary()
 {
     foreach (ColorLibrary.Name name in ColorLibrary.Library.Keys)
     {
         //arrange
         TestColor testColor = ColorLibrary.Library[name];
         //act
         HSV result = ConvertColors.ToHSV(testColor.Color);
         //assert
         Assert.AreEqual(testColor.HSV.Hue, result.Hue);
         Assert.AreEqual(testColor.HSV.Saturation, result.Saturation);
         Assert.AreEqual(testColor.HSV.Value, result.Value);
     }
 }
コード例 #6
0
        /// <summary>
        /// Returns the palest (closest to white) color in the set.
        /// </summary>
        public static Color FindPalestColor(HashSet <ColorAtPoint> points)
        {
            if (ColorIsGreyscale(points.First().Color))
            {
                return(Color.White);
            }

            Color color = Color.Black;
            HSV   hsv   = ConvertColors.ToHSV(color);

            foreach (ColorAtPoint p in points)
            {
                HSV pHSV = ConvertColors.ToHSV(p.Color);
                if (pHSV.Value > hsv.Value)
                {
                    color = p.Color;
                    hsv   = pHSV;
                }
            }
            return(color);
        }
コード例 #7
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);
        }
コード例 #8
0
        public SaturationValuePanel(Color?startColor = null)
        {
            int totalColorHeight = UNIT * 3;

            this.Height      = TRACKBAR_HEIGHT + totalColorHeight;
            this.Width       = TRACKBAR_HEIGHT + totalColorHeight;
            this.BorderStyle = BorderStyle.Fixed3D;

            saturationTrackBar               = new TrackBar();
            saturationTrackBar.Location      = new Point(TRACKBAR_HEIGHT, 0);
            saturationTrackBar.Size          = new Size(totalColorHeight, TRACKBAR_HEIGHT);
            saturationTrackBar.Minimum       = 0;
            saturationTrackBar.Maximum       = 100;
            saturationTrackBar.ValueChanged += new EventHandler(Saturation_OnValueChanged);
            this.Controls.Add(saturationTrackBar);

            valueTrackBar               = new TrackBar();
            valueTrackBar.Location      = new Point(0, TRACKBAR_HEIGHT);
            valueTrackBar.Size          = new Size(TRACKBAR_HEIGHT, totalColorHeight);
            valueTrackBar.Minimum       = 0;
            valueTrackBar.Maximum       = 100;
            valueTrackBar.Orientation   = Orientation.Vertical;
            valueTrackBar.ValueChanged += new EventHandler(Value_OnValueChanged);
            this.Controls.Add(valueTrackBar);

            gradientPanel               = new SaturationValueGradientPanel(totalColorHeight);
            gradientPanel.Location      = new Point(TRACKBAR_HEIGHT, TRACKBAR_HEIGHT);
            gradientPanel.ColorChanged += new EventHandler(Gradient_OnColorChange);
            this.Controls.Add(gradientPanel);

            if (startColor.HasValue)
            {
                HSV hsv = ConvertColors.ToHSV(startColor.Value);
                Hue        = (int)hsv.Hue;
                Saturation = (int)(hsv.Saturation * 100);
                Value      = (int)(hsv.Value * 100);
            }
        }
コード例 #9
0
        private void DisplayData()
        {
            if (color == null)
            {
                hexadecimalData.Text = "";
                rgbData.Text         = "";
                hsvData.Text         = "";
                return;
            }

            SetTextWithoutEvent(hexadecimalData, ConvertColors.ToHexadecimal(color.Value));
            SetTextWithoutEvent(rgbData, String.Format("({0}, {1}, {2})", color.Value.R, color.Value.G, color.Value.B));
            HSV hsv = ConvertColors.ToHSV(color.Value);

            SetTextWithoutEvent(hsvData, String.Format("({0:0.####}, {1:0.####}, {2:0.####})", hsv.Hue, hsv.Saturation, hsv.Value));

            if (!hexadecimalData.ReadOnly)
            {
                hexadecimalData.BackColor = COLOR_NO_ERROR;
                rgbData.BackColor         = COLOR_NO_ERROR;
                hsvData.BackColor         = COLOR_NO_ERROR;
            }
        }