Пример #1
0
    public static HSV FromColor(Color color)
    {
        HSV ret = new HSV(0f, 0f, 0f, color.a);
        float r = color.r;
        float g = color.g;
        float b = color.b;
        float K = 0f;

        if (g < b)
        {
            swap(ref g, ref b);
            K = -1f;
        }

        if (r < g)
        {
            swap(ref r, ref g);
            K = -2f / 6f - K;
        }

        float chroma = r - Mathf.Min(g, b);
        ret.h = Mathf.Abs(K + (g - b) / (6f * chroma + 1e-20f));
        ret.s = chroma / (r + 1e-20f);
        ret.v = r;
        return ret;
    }
Пример #2
0
    public static HSV GetHSV(this Color self)
    {
        HSV hsv = new HSV();
        float min = Mathf.Min(self.r, self.g, self.b);
        float max = Mathf.Max(self.r, self.g, self.b);

        hsv.v = max;               // v
        
        if (max != 0)
        {
            float delta = max - min;
            hsv.s = delta / max;       // s
            
            if (self.r == max)
                hsv.h = (self.g - self.b) / delta;       // between yellow & magenta
            else if (self.g == max)
                hsv.h = 2 + (self.b - self.r) / delta;   // between cyan & yellow
            else
                hsv.h = 4 + (self.r - self.g) / delta;   // between magenta & cyan

            hsv.h *= 60;               // degrees
            if (hsv.h < 0)
                hsv.h += 360;
        }
        else
        {
            // r = g = b = 0		// s = 0, h is undefined
            hsv.s = 0;
            hsv.h = -1;
        }

        return hsv;
    }
Пример #3
0
        /// <summary>
        /// Colour Blending: http://www.stuartdenman.com/improved-color-blending/
        /// Where blend value is [0-1]
        /// </summary>
        private HSV BlendColour(HSV one, HSV two, double blendvalue)
        {
            HSV blend = new HSV();

            double invBlendValue = 1.0f - blendvalue;
            blend.Saturation = (one.Saturation * invBlendValue) + (two.Saturation * blendvalue);
            blend.Value = (one.Value * invBlendValue) + (two.Value * blendvalue);

            double hue1 = one.Hue;
            double hue2 = two.Hue;
            double difference = hue2 - hue1;

            if (hue1 > hue2)
            {
                hue2 = one.Hue;
                hue1 = two.Hue;

                blendvalue = invBlendValue;
                difference = -difference;
            }

            if(difference > 180)
            {
                hue1 += 360.0f;
                blend.Hue = ((int)(hue1 + (blendvalue * (hue2 - hue1)))) % 360;
            }
            else
            {
                blend.Hue = hue1 + (blendvalue * difference);
            }

            return blend;
        }
Пример #4
0
        public static Color HSVtoColor(HSV hsv)
        {
            if (hsv == null)
                return Color.Blue;

            DRColor.RGB RGB = new RGB(hsv);
            return Color.FromArgb(RGB.Red, RGB.Green, RGB.Blue);
        }
Пример #5
0
        public PixelRope(Pixel1D logicalDevice)
            : base(logicalDevice)
        {
            logicalDevice.PixelChanged += (sender, e) =>
                {
                    // Handles brightness as well

                    var hsv = new HSV(e.NewColor);
                    hsv.Value = hsv.Value * e.NewBrightness;
                    var color = hsv.Color;

                    if (System.Threading.Monitor.TryEnter(lockObject))
                    {
                        try
                        {
                            PixelOutputPort.SendPixelValue(e.Channel, color.R, color.G, color.B);
                        }
                        finally
                        {
                            System.Threading.Monitor.Exit(lockObject);
                        }
                    }
                    else
                        Console.WriteLine("Missed PixelChanged in PixelRobe");
                };

            logicalDevice.MultiPixelChanged += (sender, e) =>
                {
                    byte[] values = new byte[e.NewValues.Length * 4 - 1];
                    for (int i = 0; i < e.NewValues.Length; i++)
                    {
                        var hsv = new HSV(e.NewValues[i].Color);
                        hsv.Value = hsv.Value * e.NewValues[i].Brightness;
                        var color = hsv.Color;

                        values[i * 4 + 0] = color.R;
                        values[i * 4 + 1] = color.G;
                        values[i * 4 + 2] = color.B;
                        if (i < e.NewValues.Length - 1)
                            values[i * 4 + 3] = 32;     // Delimiter
                    }

                    if (System.Threading.Monitor.TryEnter(lockObject))
                    {
                        try
                        {
                            PixelOutputPort.SendPixelsValue(e.StartChannel, values);
                        }
                        finally
                        {
                            System.Threading.Monitor.Exit(lockObject);
                        }
                    }
                    else
                        Console.WriteLine("Missed send to PixelRope");
                };
        }
Пример #6
0
        public static HSV GetHSV(Color color)
        {
            HSV hsv = new HSV();

            int max = Math.Max(color.R, Math.Max(color.G, color.B));
            int min = Math.Min(color.R, Math.Min(color.G, color.B));

            hsv.H = color.GetHue();
            hsv.S = (max == 0) ? 0 : 1d - (1d * min / max);
            hsv.V = max / 255d;

            return hsv;
        }
Пример #7
0
        public PixelRGB(ColorDimmer logicalDevice, int channel)
            : base(logicalDevice)
        {
            logicalDevice.ColorChanged += (sender, e) =>
                {
                    // Handles brightness as well

                    var hsv = new HSV(e.NewColor);
                    hsv.Value = hsv.Value * e.NewBrightness;
                    var color = hsv.Color;

                    PixelOutputPort.SendPixelValue(channel, color.R, color.G, color.B);
                };
        }
Пример #8
0
        public RGBStrobe(ColorDimmer logicalDevice, int dmxChannel)
            : base(logicalDevice)
        {
            logicalDevice.ColorChanged += (sender, e) =>
                {
                    // Handles brightness as well

                    var hsv = new HSV(e.NewColor);
                    hsv.Value = hsv.Value * e.NewBrightness;
                    var color = hsv.Color;

                    DmxOutputPort.SendDimmerValues(dmxChannel, color.R, color.G, color.B);
                };
        }
Пример #9
0
    public static HSV Lerp(HSV a, HSV b, float t)
    {
        float h, s;

        //check special case black (color.b==0): interpolate neither hue nor saturation!
        //check special case grey (color.s==0): don't interpolate hue!
        if (a.v == 0)
        {
            h = b.h;
            s = b.s;
        }
        else if (b.v == 0)
        {
            h = a.h;
            s = a.s;
        }
        else
        {
            if (a.s == 0)
            {
                h = b.h;
            }
            else if (b.s == 0)
            {
                h = a.h;
            }
            else
            {
                // works around bug with LerpAngle
                float angle = Mathf.LerpAngle(a.h * 360f, b.h * 360f, t);
                while (angle < 0f)
                    angle += 360f;
                while (angle > 360f)
                    angle -= 360f;
                h = angle / 360f;
            }
            s = Mathf.Lerp(a.s, b.s, t);
        }
        return new HSV(h, s, Mathf.Lerp(a.v, b.v, t), Mathf.Lerp(a.a, b.a, t));
    }
Пример #10
0
        public static System.Drawing.Color GetColorFromColorBrightness(ColorBrightness colorBrightness)
        {
            var hsv = new HSV(colorBrightness.Color);

            double whiteOut = Executor.Current.Whiteout.Value;

            // Adjust brightness
            double adjustedValue = (hsv.Value * colorBrightness.Brightness) + whiteOut;

            // Adjust for WhiteOut
            HSV baseHsv;
            if (colorBrightness.Brightness == 0 && whiteOut > 0)
                // Base it on black instead
                baseHsv = HSV.Black;
            else
                baseHsv = hsv;

            hsv.Saturation = baseHsv.Saturation + (HSV.White.Saturation - baseHsv.Saturation) * whiteOut;
            hsv.Value = adjustedValue.Limit(0, 1) * (1 - Executor.Current.Blackout.Value);

            return hsv.Color;
        }
Пример #11
0
 public static Color addh(this Color c, float h)
 {
     HSV hsv = new HSV(c);
     return hsv.RotateH((h + hsv.h) % 1f).ToColor();
 }
Пример #12
0
        private Color GetColorFromColorAndBrightness(Color input, double brightness)
        {
            var hsv = new HSV(input);

            // Adjust brightness
            double adjustedValue = hsv.Value * brightness;

            hsv.Value = adjustedValue.Limit(0, 1);

            return hsv.Color;
        }
Пример #13
0
        public void ShowBuffer()
        {
            var output = new Color[PixelWidth, PixelHeight];

            for (int x = 0; x < PixelWidth; x++)
                for (int y = 0; y < PixelHeight; y++)
                {
                    var hsv = new HSV(this.color[x, y]);
                    hsv.Value = hsv.Value * this.brightness[x, y] * this.globalBrightness.Value;

                    output[x, y] = hsv.Color;
                }

            this.showBuffer.OnNext(output);
        }
Пример #14
0
 public static HSV ColorToHSV(Color color)
 {
     int max = Math.Max(color.R, Math.Max(color.G, color.B));
     int min = Math.Min(color.R, Math.Min(color.G, color.B));
     HSV result = new HSV();
     {
         result.Hue = color.GetHue();
         result.Saturation = (max == 0) ? 0 : 1.0 - (1.0 * min / max);
         result.Value = max / 255.0;
     }
     return result;
 }
Пример #15
0
            public static Color HSVToColor(HSV hsv)
            {
                int hi = 0;
                double f = 0;

                {
                    hi = Convert.ToInt32(Math.Floor(hsv.Hue / 60)) % 6;
                    f = hsv.Hue / 60 - Math.Floor(hsv.Hue / 60);
                    hsv.Value = hsv.Value * 255;
                    int v = Convert.ToInt32(hsv.Value);
                    int p = Convert.ToInt32(hsv.Value * (1 - hsv.Saturation));
                    int q = Convert.ToInt32(hsv.Value * (1 - f * hsv.Saturation));
                    int t = Convert.ToInt32(hsv.Value * (1 - (1 - f) * hsv.Saturation));

                    if (hi == 0)
                    {
                        return Color.FromArgb(255, v, t, p);
                    }
                    else if (hi == 1)
                    {
                        return Color.FromArgb(255, q, v, p);
                    }
                    else if (hi == 2)
                    {
                        return Color.FromArgb(255, p, v, t);
                    }
                    else if (hi == 3)
                    {
                        return Color.FromArgb(255, p, q, v);
                    }
                    else if (hi == 4)
                    {
                        return Color.FromArgb(255, t, p, v);
                    }
                    else
                    {
                        return Color.FromArgb(255, v, p, q);
                    }
                }
            }
Пример #16
0
 public bool Contains(HSV hsv)
 {
     return skinHSV.Contains(hsv);
 }
Пример #17
0
 public static Color HSV2RGB(HSV color)
 {
     return HSV2RGB(color.h, color.s, color.v);
 }
Пример #18
0
        /// <summary>
        /// Converts a HSV color values to an RGB color
        /// </summary>
        /// <param name="hsv">HSV color to convert</param>
        /// <returns>RGB color equivalent</returns>
        public static ARGB HSVtoRGB(HSV hsv)
        {
            double r = 0.0d, g = 0.0d, b = 0.0d;
            var h = ((double)hsv.Hue / 255 * 360) % 360;
            var s = (double)hsv.Saturation / 255;
            var v = (double)hsv.Value / 255;

            if (Equals(s, 0.0d))
            {
                r = v;
                g = v;
                b = v;
            }
            else
            {
                var sectorPos = h / 60;
                var sectorNumber = (int)(Math.Floor(sectorPos));
                var fractionalSector = sectorPos - sectorNumber;
                var p = v * (1 - s);
                var q = v * (1 - (s * fractionalSector));
                var t = v * (1 - (s * (1 - fractionalSector)));
                switch (sectorNumber)
                {
                    case 0:
                        r = v;
                        g = t;
                        b = p;
                        break;

                    case 1:
                        r = q;
                        g = v;
                        b = p;
                        break;

                    case 2:
                        r = p;
                        g = v;
                        b = t;
                        break;

                    case 3:
                        r = p;
                        g = q;
                        b = v;
                        break;

                    case 4:
                        r = t;
                        g = p;
                        b = v;
                        break;

                    case 5:
                        r = v;
                        g = p;
                        b = q;
                        break;
                }
            }
            return new ARGB(hsv.Alpha, (int)(r * 255), (int)(g * 255), (int)(b * 255));
        }
Пример #19
0
        public static ARGB HSVtoRGB(HSV HSV)
        {
            // HSV contains values scaled as in the color wheel:
            // that is, all from 0 to 255.

            // for ( this code to work, HSV.Hue needs
            // to be scaled from 0 to 360 (it//s the angle of the selected
            // point within the circle). HSV.Saturation and HSV.value must be
            // scaled to be between 0 and 1.

            double h;
            double s;
            double v;

            double r = 0;
            double g = 0;
            double b = 0;

            // Scale Hue to be between 0 and 360. Saturation
            // and value scale to be between 0 and 1.
            h = ((double) HSV.Hue/255*360)%360;
            s = (double) HSV.Saturation/255;
            v = (double) HSV.Value/255;

            if (s == 0)
            {
                // If s is 0, all colors are the same.
                // This is some flavor of gray.
                r = v;
                g = v;
                b = v;
            }
            else
            {
                // The color wheel consists of 6 sectors.
                // Figure out which sector you//re in.
                double sectorPos = h/60;
                int sectorNumber = (int) (Math.Floor(sectorPos));

                // get the fractional part of the sector.
                // That is, how many degrees into the sector
                // are you?
                double fractionalSector = sectorPos - sectorNumber;

                // Calculate values for the three axes
                // of the color.
                double p = v*(1 - s);
                double q = v*(1 - (s*fractionalSector));
                double t = v*(1 - (s*(1 - fractionalSector)));

                // Assign the fractional colors to r, g, and b
                // based on the sector the angle is in.
                switch (sectorNumber)
                {
                    case 0:
                        r = v;
                        g = t;
                        b = p;
                        break;

                    case 1:
                        r = q;
                        g = v;
                        b = p;
                        break;

                    case 2:
                        r = p;
                        g = v;
                        b = t;
                        break;

                    case 3:
                        r = p;
                        g = q;
                        b = v;
                        break;

                    case 4:
                        r = t;
                        g = p;
                        b = v;
                        break;

                    case 5:
                        r = v;
                        g = p;
                        b = q;
                        break;
                }
            }
            // return an RGB structure, with values scaled
            // to be between 0 and 255.
            return new ARGB(HSV.Alpha, (int) (r*255), (int) (g*255), (int) (b*255));
        }
Пример #20
0
        /// <summary>
        /// Converting RGB-HSV: http://www.poynton.com/PDFs/coloureq.pdf p15
        /// Where rgb is [0,1]
        /// </summary>
        private HSV ConvertColour(RGB rgb)
        {
            HSV hsv = new HSV();

            double min = Math.Min(Math.Min(rgb.Red, rgb.Blue), rgb.Green);
            double max = Math.Max(Math.Max(rgb.Red, rgb.Blue), rgb.Green);
            hsv.Saturation = max == 0.0f ? 0.0f : (max - min) / max;
            hsv.Value = max;

            double red = (max - rgb.Red) / (max - min);
            double green = (max - rgb.Green) / (max - min);
            double blue = (max - rgb.Blue) / (max - min);

            if (hsv.Saturation != 0)
            {
                if (rgb.Red == max && rgb.Green == min)
                {
                    hsv.Hue = 5.0f + blue;
                }
                else if (rgb.Red == max && rgb.Green != min)
                {
                    hsv.Hue = 1.0f - green;
                }
                else if (rgb.Green == max && rgb.Blue == min)
                {
                    hsv.Hue = 1.0f + red;
                }
                else if (rgb.Green == max && rgb.Blue != min)
                {
                    hsv.Hue = 3.0f - blue;
                }
                else if (rgb.Red == max || rgb.Red == min)
                {
                    hsv.Hue = 3.0f + green;
                }
                else
                {
                    hsv.Hue = 5.0f - red;
                }
            }

            hsv.Hue *= 60.0f;
            return hsv;
        }
Пример #21
0
 public static Color adds(this Color c, float s)
 {
     HSV hsv = new HSV(c);
     return hsv.S(s + hsv.s).ToColor();
 }
Пример #22
0
        /// <summary>
        /// Converting HSV-RGB: http://www.poynton.com/PDFs/coloureq.pdf p15
        /// Where h is [0,360] and sv is [0,1]
        /// </summary>
        private RGB ConvertColour(HSV hsv)
        {
            RGB rgb = new RGB();

            double hex = hsv.Hue / 60.0f;
            int primary = (int)Math.Floor(hex);
            double secondary = hex - primary;
            double a = (1.0f - hsv.Saturation) * hsv.Value;
            double b = (1.0f - (hsv.Saturation * secondary)) * hsv.Value;
            double c = (1.0f - (hsv.Saturation * (1.0f - secondary))) * hsv.Value;

            switch(primary)
            {
            case 0:
                rgb.Red = hsv.Value;
                rgb.Green = c;
                rgb.Blue = a;
                break;
            case 1:
                rgb.Red = b;
                rgb.Green = hsv.Value;
                rgb.Blue = a;
                break;
            case 2:
                rgb.Red = a;
                rgb.Green = hsv.Value;
                rgb.Blue = c;
                break;
            case 3:
                rgb.Red = a;
                rgb.Green = b;
                rgb.Blue = hsv.Value;
                break;
            case 4:
                rgb.Red = c;
                rgb.Green = a;
                rgb.Blue = hsv.Value;
                break;
            case 5:
                rgb.Red = hsv.Value;
                rgb.Green = a;
                rgb.Blue = b;
                break;
            case 6:
                rgb.Red = hsv.Value;
                rgb.Green = c;
                rgb.Blue = a;
                break;
            }

            return rgb;
        }
Пример #23
0
 public static Color addv(this Color c, float v)
 {
     HSV hsv = new HSV(c);
     return hsv.V(v + hsv.v).ToColor();
 }
Пример #24
0
 public void SetColor(Color c)
 {
     this.rgbColor = c;
     this.hsvColor = GetHSV(this.rgbColor);
     this.colorRbox.Value = this.rgbColor.R;
     this.colorGbox.Value = this.rgbColor.G;
     this.colorBbox.Value = this.rgbColor.B;
     this.colorBox.BackColor = this.rgbColor;
     this.UpdatePalette();
 }
Пример #25
0
 public static Color HSVToColor(HSV hsv, int alpha = 255)
 {
     return ColorFromHSV (hsv.H, hsv.S, hsv.V, alpha);
 }
Пример #26
0
 public void SetColorRGB(int R, int G, int B)
 {
     this.rgbColor = Color.FromArgb(R, G, B);
     this.hsvColor = GetHSV(this.rgbColor);
     this.colorRbox.Value = this.rgbColor.R;
     this.colorGbox.Value = this.rgbColor.G;
     this.colorBbox.Value = this.rgbColor.B;
     this.colorBox.BackColor = this.rgbColor;
     this.UpdatePalette();
 }
Пример #27
0
 /// <summary>
 /// Converts a HSV color to a System.Drawing.Color
 /// </summary>
 /// <param name="hsv">HSV color to convert</param>
 /// <returns>System.Drawing.Color equivalent</returns>
 public static Color HSVtoColor(HSV hsv)
 {
     var argb = HSVtoRGB(hsv);
     return Color.FromArgb(argb.Alpha, argb.Red, argb.Green, argb.Blue);
 }
Пример #28
0
 public static Color HSVtoColor(HSV hsv)
 {
     RGB RGB = HSVtoRGB(hsv);
             return Color.FromArgb(RGB.Alpha, RGB.Red, RGB.Green, RGB.Blue);
 }
Пример #29
0
        public Pixel1D FadeToUsingHSV(int channel, Color color, double brightness, TimeSpan duration)
        {
            if (channel < 0 || channel >= this.pixelCount)
                throw new ArgumentOutOfRangeException();

            if (this.color[channel].GetBrightness() == 0)
            {
                this.color[channel] = color;
                this.brightness[channel] = 0;
            }

            if (color.GetBrightness() == 0)
            {
                color = this.color[channel];
                brightness = 0;
            }

            var startHSV = new HSV(this.color[channel]);
            var endHSV = new HSV(color);
            double startBrightness = this.brightness[channel];

            // 10 steps per second
            int steps = (int)(duration.TotalMilliseconds / 100);

            double position = 0;
            for (int i = 0; i < steps; i++)
            {
                double newBrightness = startBrightness + (brightness - startBrightness) * position;

                double hue = startHSV.Hue + (endHSV.Hue - startHSV.Hue) * position;
                double sat = startHSV.Saturation + (endHSV.Saturation - startHSV.Saturation) * position;
                double val = startHSV.Value + (endHSV.Value - startHSV.Value) * position;
                Color newColor = HSV.ColorFromHSV(hue, sat, val);

                SetColor(channel, newColor, newBrightness);

                System.Threading.Thread.Sleep(100);

                position += 1.0 / (steps - 1);
            }

            return this;
        }
Пример #30
0
            public static HSV RGB2HSV(double r, double b, double g)
            {
                double delta, min;
                double h = 0, s, v;

                min = Math.Min(Math.Min(r, g), b);
                v = Math.Max(Math.Max(r, g), b);
                delta = v - min;

                if (v == 0.0)
                {
                    s = 0;

                }
                else
                    s = delta / v;

                if (s == 0)
                    h = 0.0f;

                else
                {
                    if (r == v)
                        h = (g - b) / delta;
                    else if (g == v)
                        h = 2 + (b - r) / delta;
                    else if (b == v)
                        h = 4 + (r - g) / delta;

                    h *= 60;
                    if (h < 0.0)
                        h = h + 360;

                }

                HSV hsvColor = new HSV();
                hsvColor.h = h;
                hsvColor.s = s;
                hsvColor.v = v / 255;

                return hsvColor;
            }