Ejemplo n.º 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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
            }
        }
 public void HexadecimalFromColor_ColorLibrary()
 {
     foreach (ColorLibrary.Name name in ColorLibrary.Library.Keys)
     {
         //arrange
         TestColor testColor = ColorLibrary.Library[name];
         //act
         string result = ConvertColors.ToHexadecimal(testColor.Color);
         //assert
         Assert.AreEqual(testColor.Hexadecimal, result);
     }
 }
        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());
            }
        }
 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);
     }
 }
 public void ColorFromHexadecimal_ColorLibrary()
 {
     foreach (ColorLibrary.Name name in ColorLibrary.Library.Keys)
     {
         //arrange
         TestColor testColor = ColorLibrary.Library[name];
         //act
         Color result = ConvertColors.HexadecimalToColor(testColor.Hexadecimal);
         //assert
         Assert.AreEqual(testColor.Color.R, result.R);
         Assert.AreEqual(testColor.Color.G, result.G);
         Assert.AreEqual(testColor.Color.B, result.B);
     }
 }
        public void TryParseRGB_255_255_25()
        {
            //arrange
            int    red   = 255;
            int    green = 255;
            int    blue  = 25;
            string text  = String.Format("{0},{1},{2}", red, green, blue);
            Color  color = Color.Black;

            //act
            ConvertColors.TryParseRGB(text, out color);
            //assert
            Assert.AreEqual(red, color.R);
            Assert.AreEqual(green, color.G);
            Assert.AreEqual(blue, color.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);
        }
Ejemplo n.º 10
0
        private void OnTextChanged(object sender, EventArgs e)
        {
            TextBox textBox  = sender as TextBox;
            Color   newColor = System.Drawing.Color.Black;
            bool    success  = false;

            switch (textBox.Name)
            {
            case NAME_HEXADECIMAL:
                if ((textBox.Text.Length == 6 && !textBox.Text.StartsWith("#")) || (textBox.Text.Length == 7 && textBox.Text.StartsWith("#")))
                {
                    success = ConvertColors.TryParseHexadecimal(textBox.Text, out newColor);
                }
                break;

            case NAME_RGB:
                if (textBox.Text.EndsWith(")"))
                {
                    success = ConvertColors.TryParseRGB(textBox.Text, out newColor);
                }
                break;

            case NAME_HSV:
                if (textBox.Text.EndsWith(")"))
                {
                    success = ConvertColors.TryParseHSV(textBox.Text, out newColor);
                }
                break;
            }
            if (success)
            {
                Color = newColor;
                if (ColorChanged != null)
                {
                    ColorChanged(this, new ColorEventArgs(newColor));
                }
            }
            else
            {
                textBox.BackColor = COLOR_ERROR;
            }
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
        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);
            }
        }
Ejemplo n.º 14
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;
            }
        }
Ejemplo n.º 15
0
        public void GenerateGradiantSamples()
        {
            List <Color> colors = new List <Color>()
            {
                //100% saturation, 100% value
                ConvertColors.HSVToColor(0, 1, 1),                 //red
                ConvertColors.HSVToColor(30, 1, 1),                //orange
                ConvertColors.HSVToColor(60, 1, 1),                //yellow
                ConvertColors.HSVToColor(90, 1, 1),                //green
                ConvertColors.HSVToColor(120, 1, 1),               //green also
                ConvertColors.HSVToColor(150, 1, 1),               //aqua
                ConvertColors.HSVToColor(180, 1, 1),               //cyan
                ConvertColors.HSVToColor(210, 1, 1),               //blue
                ConvertColors.HSVToColor(240, 1, 1),               //blue darker
                ConvertColors.HSVToColor(270, 1, 1),               //purple
                ConvertColors.HSVToColor(300, 1, 1),               //magenta
                ConvertColors.HSVToColor(330, 1, 1),               //fushia
                //100% saturation, 50% value
                ConvertColors.HSVToColor(0, 1, 0.5f),              //red
                ConvertColors.HSVToColor(30, 1, 0.5f),             //orange
                ConvertColors.HSVToColor(60, 1, 0.5f),             //yellow
                ConvertColors.HSVToColor(90, 1, 0.5f),             //green
                ConvertColors.HSVToColor(120, 1, 0.5f),            //green also
                ConvertColors.HSVToColor(150, 1, 0.5f),            //aqua
                ConvertColors.HSVToColor(180, 1, 0.5f),            //cyan
                ConvertColors.HSVToColor(210, 1, 0.5f),            //blue
                ConvertColors.HSVToColor(240, 1, 0.5f),            //blue darker
                ConvertColors.HSVToColor(270, 1, 0.5f),            //purple
                ConvertColors.HSVToColor(300, 1, 0.5f),            //magenta
                ConvertColors.HSVToColor(330, 1, 0.5f),            //fushia
                //100% saturation, 25% value
                ConvertColors.HSVToColor(0, 1, 0.25f),             //red
                ConvertColors.HSVToColor(30, 1, 0.25f),            //orange
                ConvertColors.HSVToColor(60, 1, 0.25f),            //yellow
                ConvertColors.HSVToColor(90, 1, 0.25f),            //green
                ConvertColors.HSVToColor(120, 1, 0.25f),           //green also
                ConvertColors.HSVToColor(150, 1, 0.25f),           //aqua
                ConvertColors.HSVToColor(180, 1, 0.25f),           //cyan
                ConvertColors.HSVToColor(210, 1, 0.25f),           //blue
                ConvertColors.HSVToColor(240, 1, 0.25f),           //blue darker
                ConvertColors.HSVToColor(270, 1, 0.25f),           //purple
                ConvertColors.HSVToColor(300, 1, 0.25f),           //magenta
                ConvertColors.HSVToColor(330, 1, 0.25f),           //fushia
                //50% saturation, 100% value
                ConvertColors.HSVToColor(0, 0.5f, 1),              //red
                ConvertColors.HSVToColor(30, 0.5f, 1),             //orange
                ConvertColors.HSVToColor(60, 0.5f, 1),             //yellow
                ConvertColors.HSVToColor(90, 0.5f, 1),             //green
                ConvertColors.HSVToColor(120, 0.5f, 1),            //green also
                ConvertColors.HSVToColor(150, 0.5f, 1),            //aqua
                ConvertColors.HSVToColor(180, 0.5f, 1),            //cyan
                ConvertColors.HSVToColor(210, 0.5f, 1),            //blue
                ConvertColors.HSVToColor(240, 0.5f, 1),            //blue darker
                ConvertColors.HSVToColor(270, 0.5f, 1),            //purple
                ConvertColors.HSVToColor(300, 0.5f, 1),            //magenta
                ConvertColors.HSVToColor(330, 0.5f, 1),            //fushia
                //50% saturation, 50% value
                ConvertColors.HSVToColor(0, 0.5f, 0.5f),           //red
                ConvertColors.HSVToColor(30, 0.5f, 0.5f),          //orange
                ConvertColors.HSVToColor(60, 0.5f, 0.5f),          //yellow
                ConvertColors.HSVToColor(90, 0.5f, 0.5f),          //green
                ConvertColors.HSVToColor(120, 0.5f, 0.5f),         //green also
                ConvertColors.HSVToColor(150, 0.5f, 0.5f),         //aqua
                ConvertColors.HSVToColor(180, 0.5f, 0.5f),         //cyan
                ConvertColors.HSVToColor(210, 0.5f, 0.5f),         //blue
                ConvertColors.HSVToColor(240, 0.5f, 0.5f),         //blue darker
                ConvertColors.HSVToColor(270, 0.5f, 0.5f),         //purple
                ConvertColors.HSVToColor(300, 0.5f, 0.5f),         //magenta
                ConvertColors.HSVToColor(330, 0.5f, 0.5f),         //fushia
                //50% saturation, 50% value
                ConvertColors.HSVToColor(0, 0.5f, 0.25f),          //red
                ConvertColors.HSVToColor(30, 0.5f, 0.25f),         //orange
                ConvertColors.HSVToColor(60, 0.5f, 0.25f),         //yellow
                ConvertColors.HSVToColor(90, 0.5f, 0.25f),         //green
                ConvertColors.HSVToColor(120, 0.5f, 0.25f),        //green also
                ConvertColors.HSVToColor(150, 0.5f, 0.25f),        //aqua
                ConvertColors.HSVToColor(180, 0.5f, 0.25f),        //cyan
                ConvertColors.HSVToColor(210, 0.5f, 0.25f),        //blue
                ConvertColors.HSVToColor(240, 0.5f, 0.25f),        //blue darker
                ConvertColors.HSVToColor(270, 0.5f, 0.25f),        //purple
                ConvertColors.HSVToColor(300, 0.5f, 0.25f),        //magenta
                ConvertColors.HSVToColor(330, 0.5f, 0.25f),        //fushia
                //25% saturation, 100% value
                ConvertColors.HSVToColor(0, 0.25f, 1),             //red
                ConvertColors.HSVToColor(30, 0.25f, 1),            //orange
                ConvertColors.HSVToColor(60, 0.25f, 1),            //yellow
                ConvertColors.HSVToColor(90, 0.25f, 1),            //green
                ConvertColors.HSVToColor(120, 0.25f, 1),           //green also
                ConvertColors.HSVToColor(150, 0.25f, 1),           //aqua
                ConvertColors.HSVToColor(180, 0.25f, 1),           //cyan
                ConvertColors.HSVToColor(210, 0.25f, 1),           //blue
                ConvertColors.HSVToColor(240, 0.25f, 1),           //blue darker
                ConvertColors.HSVToColor(270, 0.25f, 1),           //purple
                ConvertColors.HSVToColor(300, 0.25f, 1),           //magenta
                ConvertColors.HSVToColor(330, 0.25f, 1),           //fushia
                //25% saturation, 75% value
                ConvertColors.HSVToColor(0, 0.25f, 0.75f),         //red
                ConvertColors.HSVToColor(30, 0.25f, 0.75f),        //orange
                ConvertColors.HSVToColor(60, 0.25f, 0.75f),        //yellow
                ConvertColors.HSVToColor(90, 0.25f, 0.75f),        //green
                ConvertColors.HSVToColor(120, 0.25f, 0.75f),       //green also
                ConvertColors.HSVToColor(150, 0.25f, 0.75f),       //aqua
                ConvertColors.HSVToColor(180, 0.25f, 0.75f),       //cyan
                ConvertColors.HSVToColor(210, 0.25f, 0.75f),       //blue
                ConvertColors.HSVToColor(240, 0.25f, 0.75f),       //blue darker
                ConvertColors.HSVToColor(270, 0.25f, 0.75f),       //purple
                ConvertColors.HSVToColor(300, 0.25f, 0.75f),       //magenta
                ConvertColors.HSVToColor(330, 0.25f, 0.75f),       //fushia
                //25% saturation, 50% value
                ConvertColors.HSVToColor(0, 0.25f, 0.5f),          //red
                ConvertColors.HSVToColor(30, 0.25f, 0.5f),         //orange
                ConvertColors.HSVToColor(60, 0.25f, 0.5f),         //yellow
                ConvertColors.HSVToColor(90, 0.25f, 0.5f),         //green
                ConvertColors.HSVToColor(120, 0.25f, 0.5f),        //green also
                ConvertColors.HSVToColor(150, 0.25f, 0.5f),        //aqua
                ConvertColors.HSVToColor(180, 0.25f, 0.5f),        //cyan
                ConvertColors.HSVToColor(210, 0.25f, 0.5f),        //blue
                ConvertColors.HSVToColor(240, 0.25f, 0.5f),        //blue darker
                ConvertColors.HSVToColor(270, 0.25f, 0.5f),        //purple
                ConvertColors.HSVToColor(300, 0.25f, 0.5f),        //magenta
                ConvertColors.HSVToColor(330, 0.25f, 0.5f),        //fushia
                //25% saturation, 25% value
                ConvertColors.HSVToColor(0, 0.25f, 0.25f),         //red
                ConvertColors.HSVToColor(30, 0.25f, 0.25f),        //orange
                ConvertColors.HSVToColor(60, 0.25f, 0.25f),        //yellow
                ConvertColors.HSVToColor(90, 0.25f, 0.25f),        //green
                ConvertColors.HSVToColor(120, 0.25f, 0.25f),       //green also
                ConvertColors.HSVToColor(150, 0.25f, 0.25f),       //aqua
                ConvertColors.HSVToColor(180, 0.25f, 0.25f),       //cyan
                ConvertColors.HSVToColor(210, 0.25f, 0.25f),       //blue
                ConvertColors.HSVToColor(240, 0.25f, 0.25f),       //blue darker
                ConvertColors.HSVToColor(270, 0.25f, 0.25f),       //purple
                ConvertColors.HSVToColor(300, 0.25f, 0.25f),       //magenta
                ConvertColors.HSVToColor(330, 0.25f, 0.25f),       //fushia
            };
            int width = 50;

            foreach (Color color in colors)
            {
                Bitmap revertBitmap = GetRevertedGradient(width, color);
                Bitmap colorBitmap  = GetColoredGradient(width, color);
                Bitmap grayBitmap   = GetGrayscaleGradient(width);
                Bitmap bitmap       = new Bitmap(width * 3, revertBitmap.Height);
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    graphics.DrawImage(revertBitmap, new Point(0, 0));
                    graphics.DrawImage(colorBitmap, new Point(width, 0));
                    graphics.DrawImage(grayBitmap, new Point(width * 2, 0));
                }
                bitmap.Save(String.Format("GradientSamples/sample_{0:000}_{1:000}_{2:000}.png", color.R, color.G, color.B));
            }
        }
Ejemplo n.º 16
0
 private void UpdateSelectedColor()
 {
     Color = ConvertColors.ToColor(new HSV(huePanel.Hue, saturationValuePanel.Saturation / 100f, saturationValuePanel.Value / 100f));
     selectedColorPanel.BackColor = Color;
     colorDataPanel.Color         = Color;
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Converts translucent colors to a greyscale equivalent.
 /// </summary>
 private static Color ConvertPartiallyClearToGrey(Color oldColor)
 {
     //25% solid => 75% gray
     return(ConvertColors.HSVToColor(0, 0, (255 - oldColor.A) / 255f));
 }