예제 #1
0
        internal static IDictionary <int, int> GetHueMAp(StandardImage image)
        {
            var map = new Dictionary <int, int>();

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    var myRgb = new Rgb {
                        R = image.R[x, y], G = image.G[x, y], B = image.B[x, y]
                    };
                    var hsl = myRgb.To <Hsv>();
                    var hue = (int)(hsl.H / 3.6);

                    if (map.ContainsKey(hue))
                    {
                        map[hue]++;
                    }
                    else
                    {
                        map.Add(hue, 1);
                    }
                }
            }

            return(map);
        }
예제 #2
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Color color = (Color)value;

            _lastColor = color;

            Rgb colrgb = new Rgb();

            colrgb.R = color.R;
            colrgb.G = color.G;
            colrgb.B = color.B;

            Hsv colhsv = colrgb.To <Hsv>();

            _lastHsv = colhsv;

            switch ((string)parameter)
            {
            case "r": return(colhsv.H);

            case "g": return(colhsv.S);

            case "b": return(colhsv.V);
                //case "a": return color.A;
            }
            return(Binding.DoNothing);
        }
        public double[] MapColors(IEnumerable <ColorValueAttribute> colors)
        {
            var colorConverter = new ColorConverter();
            var color          = colors.FirstOrDefault(c => !string.IsNullOrEmpty(c.Value) && c.Value != "#ffffff" && c.Value != "#000000");

            if (color == null)
            {
                return(NoColor);
            }

            var convertFromInvariantString = colorConverter.ConvertFromInvariantString(color.Value) as Color?;

            if (convertFromInvariantString == null)
            {
                return(NoColor);
            }

            var colorObject = convertFromInvariantString.Value;
            var rgb         = new Rgb {
                R = colorObject.R, G = colorObject.G, B = colorObject.B
            };
            var lab = rgb.To <Lab>();

            var settings = _settings.ColorAttributes[color.ID];

            return(new[]
            {
                ScaleNumber(lab.L / 100, settings.LuminescenceSettings.MinValue, settings.LuminescenceSettings.MaxValue),
                ScaleNumber(lab.A / 100, settings.ASettings.MinValue, settings.ASettings.MaxValue),
                ScaleNumber(lab.B / 100, settings.BSettings.MinValue, settings.BSettings.MaxValue)
            });
        }
예제 #4
0
        /// <summary>
        /// Returns the custom color nearest to the System color input
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static PColor FindNearestColor(this Color input)
        {
            PColor resultMSColor  = PColor.WebColors.White;
            var    resultDistance = 255.0;

            //Create ColorMine RGB and LAB variables for comparisons
            Rgb inColoRgb = new Rgb {
                R = input.R, G = input.G, B = input.B
            };
            Lab inColorLab = inColoRgb.To <Lab>();

            foreach (var pcolor in PColor.WebColors.All)
            {
                var comparerLabColor = new CieDe2000Comparison();
                var mineColorRgb     = new Rgb {
                    R = pcolor.SystemColor.R, G = pcolor.SystemColor.G, B = pcolor.SystemColor.B
                };
                Lab mineColorLab = mineColorRgb.To <Lab>();

                //Comare colors and check if closer than previous closest
                var currentDistence = inColoRgb.Compare(mineColorLab, comparerLabColor);
                if (currentDistence == 0)
                {
                    resultMSColor = pcolor;
                    break;
                }
                else if (currentDistence < resultDistance)
                {
                    resultMSColor  = pcolor;
                    resultDistance = currentDistence;
                }
            }

            return(resultMSColor);
        }
예제 #5
0
        internal static IDictionary <Point, int> GetSVMap(StandardImage image)
        {
            var map = new Dictionary <Point, int>();

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    var myRgb = new Rgb {
                        R = image.R[x, y], G = image.G[x, y], B = image.B[x, y]
                    };
                    var hsl        = myRgb.To <Hsv>();
                    var saturation = (int)(hsl.S * 100);
                    var brightness = (int)(hsl.V * 100);
                    var point      = new Point(saturation, brightness);

                    if (map.ContainsKey(point))
                    {
                        map[point]++;
                    }
                    else
                    {
                        map.Add(point, 1);
                    }
                }
            }

            return(map);
        }
예제 #6
0
        private void UpdateHsvFromRgb()
        {
            Rgb rgb = new Rgb()
            {
                R = this.red, G = this.green, B = this.blue
            };
            Hsv hsv = rgb.To <Hsv>();

            if (this.hue != (int)hsv.H)
            {
                this.hue = (int)hsv.H;
                this.OnPropertyChanged("Hue");
            }

            if (this.saturation != (int)(100.0 * hsv.S))
            {
                this.saturation = (int)(100.0 * hsv.S);
                this.OnPropertyChanged("Saturation");
            }

            if (this.value != (int)(100.0 * hsv.V))
            {
                this.value = (int)(100.0 * hsv.V);
                this.OnPropertyChanged("Value");
            }
        }
예제 #7
0
        /// <summary>
        /// Converts System.Windows.Media.Color to LCH colourspace.
        /// </summary>
        public static Lch ToLch(this Color color)
        {
            Rgb rgbColor = new Rgb {
                R = color.R, G = color.G, B = color.B
            };

            return(rgbColor.To <Lch>());
        }
예제 #8
0
        protected static void ExpectedValuesForRoundTrip <T>(Rgb knownColor) where T : IColorSpace, new()
        {
            var midColor = knownColor.To <T>();
            var target   = midColor.To <Rgb>();

            Assert.AreEqual(knownColor.R, target.R, 1.2, knownColor.ToString() + " => " + midColor.ToString() + " => " + target.ToString() + " R: " + knownColor.R + " != " + target.R);
            Assert.AreEqual(knownColor.G, target.G, 1.2, knownColor.ToString() + " => " + midColor.ToString() + " => " + target.ToString() + " G: " + knownColor.G + " != " + target.G);
            Assert.AreEqual(knownColor.B, target.B, 1.2, knownColor.ToString() + " => " + midColor.ToString() + " => " + target.ToString() + " B: " + knownColor.B + " != " + target.B);
        }
예제 #9
0
        public static Lab CreateLabFromRgb(int r, int g, int b)
        {
            var rgb = new Rgb();

            rgb.R = r;
            rgb.G = g;
            rgb.B = b;

            return(rgb.To <Lab>());
        }
예제 #10
0
        public GrayscaleStandardImage SvPikeHat(StandardImage image, IDictionary <Point, int> svPikes)
        {
            var grayImage        = image.ConvertToGrayScaleStandardImage();
            var saturationCutoff = 0;
            var brightnessCutoff = 0;

            if (svPikes.Count == 1)
            {
                saturationCutoff = svPikes.Single().Key.X + 30;
                brightnessCutoff = svPikes.Single().Key.Y - 30;
            }
            else if (svPikes.Count == 2)
            {
                saturationCutoff = (svPikes.First().Key.X + svPikes.Last().Key.X) / 2;
                brightnessCutoff = (svPikes.First().Key.Y + svPikes.Last().Key.Y) / 2;
            }
            else
            {
                var pikesList = svPikes.ToList();
                pikesList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
                var pike1 = pikesList[0];
                var pike2 = pikesList[1];
                saturationCutoff = (pike1.Key.X + pike2.Key.X) / 2;
                brightnessCutoff = (pike1.Key.Y + pike2.Key.Y) / 2;
            }

            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    var myRgb = new Rgb {
                        R = image.R[i, j], G = image.G[i, j], B = image.B[i, j]
                    };
                    var hsl = myRgb.To <Hsv>();

                    var saturation = hsl.S * 100;
                    if (saturation > 13 && saturation < 40)
                    {
                        grayImage.C[i, j] = 255;
                    }
                    else
                    {
                        grayImage.C[i, j] = 0;
                    }
                    //var saturation = hsl.S*100;
                    //var brightness = hsl.V*100;
                    //if (saturation < saturationCutoff && brightness  > brightnessCutoff)
                    //    grayImage.C[i, j] = 255;
                    //else
                    //    grayImage.C[i, j] = 0;
                }
            }

            return(grayImage);
        }
예제 #11
0
        public static void ColorToHSV(Color color, out double hue, out double saturation, out double value)
        {
            var rgb = new Rgb {
                R = Math.Round(color.R * 255F), G = Math.Round(color.G * 255F), B = Math.Round(color.B * 255F)
            };
            var hsv = rgb.To <Hsv>();

            hue        = color.Hue;
            saturation = hsv.S;
            value      = hsv.V;
        }
        private static Lab Color2Lab(Color color)
        {
            var rgb = new Rgb
            {
                R = color.R,
                G = color.G,
                B = color.B
            };

            return(rgb.To <Lab>());
        }
예제 #13
0
        public static void ConvertRgbToLab(Color color, out double L, out double A, out double B)
        {
            var myRgb = new Rgb {
                R = color.R, G = color.G, B = color.B
            };
            var myLab = myRgb.To <Lab>();

            L = myLab.L;
            A = myLab.A;
            B = myLab.B;
        }
예제 #14
0
        private void autoBtn_Click(object sender, EventArgs e)
        {
            var accentColor = WindowCustomizationUtility.GetActiveAccentColor();

            var rgb = new Rgb(accentColor.R, accentColor.G, accentColor.B);
            var hsl = rgb.To <Hsl>();

            hsl.L *= 1.65;
            rgb    = hsl.To <Rgb>();

            Red   = (int)rgb.R;
            Green = (int)rgb.G;
            Blue  = (int)rgb.B;
        }
예제 #15
0
        private static void RGBtoLAB(byte r, byte g, byte b, ref double l, ref double a, ref double labb)
        {
            Rgb myRGB = new Rgb
            {
                R = (double)r,
                G = (double)g,
                B = (double)b
            };

            Lab myLAB = myRGB.To <Lab>();

            l    = myLAB.L;
            a    = myLAB.A;
            labb = myLAB.B;
        }
예제 #16
0
        private static Lab[] ColorsToLabsCm(IReadOnlyList <Color> colors)
        {
            var labs = new Lab[colors.Count];

            for (var i = 0; i < colors.Count; i++)
            {
                var c   = colors[i];
                var rgb = new Rgb {
                    R = c.R, G = c.G, B = c.B
                };
                labs[i] = rgb.To <Lab>();
            }

            return(labs);
        }
예제 #17
0
        public Dictionary <LUVClass, float> convertToLuv(Dictionary <Color, float> histogram)
        {
            Dictionary <LUVClass, float> hist = new Dictionary <LUVClass, float>();

            foreach (Color key in histogram.Keys)
            {
                var myRgb = new Rgb {
                    R = key.R, G = key.G, B = key.B
                };
                var myLuv = myRgb.To <Luv>();

                hist.Add(new LUVClass(myLuv.L, myLuv.U, myLuv.V), histogram[key]);
            }

            return(hist);
        }
예제 #18
0
        public Task <CommandResponse> SetColorFromRGB(Rgb rgb)
        {
            Hsv hsv = rgb.To <Hsv>();

            Commands commands = new Commands();

            commands.AddCommand("work_mode", "colour");
            commands.AddCommand("colour_data_v2", new
            {
                h = Math.Round(hsv.H),
                s = Math.Round(hsv.S * 1000),
                v = Math.Round(hsv.V * 1000)
            });

            return(rumahTuya.SendCommands(deviceId, commands));
        }
예제 #19
0
 private LUVClass[,] getLuvMatrix(Bitmap img)
 {
     LUVClass[,] matrix = new LUVClass[img.Width, img.Height];
     for (int i = 0; i < img.Width; i++)
     {
         for (int j = 0; j < img.Height; j++)
         {
             Color pixel = img.GetPixel(i, j);
             var   myRgb = new Rgb {
                 R = pixel.R, G = pixel.G, B = pixel.B
             };
             var myLuv = myRgb.To <Luv>();
             matrix[i, j] = new LUVClass(myLuv.L, myLuv.U, myLuv.V);
         }
     }
     return(matrix);
 }
예제 #20
0
        internal static bool CouldBeDocumentColor(this Color c1)
        {
            var myRgb = new Rgb {
                R = c1.R, G = c1.G, B = c1.B
            };
            var hsl = myRgb.To <Hsv>();

            var saturation = hsl.S * 100;
            var brightness = hsl.V * 100;

            if (saturation < 37 && brightness > 50)
            {
                return(true);
            }

            return(false);
        }
예제 #21
0
        internal static bool IsInPike(this Color c1, SvPike pike)
        {
            var myRgb = new Rgb {
                R = c1.R, G = c1.G, B = c1.B
            };
            var hsl = myRgb.To <Hsv>();

            var saturation = hsl.S * 100;
            var brightness = hsl.V * 100;

            if (Math.Abs(pike.Saturation - saturation) < pike.SaturationRay && Math.Abs(pike.Brightness - brightness) < pike.BrightnessRay)
            {
                return(true);
            }

            return(false);
        }
예제 #22
0
 private LABClass[,] convertToLab(Bitmap img)
 {
     LABClass[,] labMatrix = new LABClass[img.Width, img.Height];
     for (int i = 0; i < img.Width; i++)
     {
         for (int j = 0; j < img.Height; j++)
         {
             Color rgb   = img.GetPixel(i, j);
             var   myRgb = new Rgb {
                 R = rgb.R, G = rgb.G, B = rgb.B
             };
             var myLuv = myRgb.To <Lab>();
             labMatrix[i, j] = new LABClass(myLuv.L, myLuv.A, myLuv.B);
         }
     }
     return(labMatrix);
 }
예제 #23
0
        internal void RGBtoLAB()
        {
            for (int y = 0; y < Height; ++y)
            {
                for (int x = 0; x < Width; ++x)
                {
                    double R = Bitplane[2].GetPixel(x, y);
                    double G = Bitplane[1].GetPixel(x, y);
                    double B = Bitplane[0].GetPixel(x, y);

                    Rgb myRGB = new Rgb(R, G, B);
                    Lab myLAB = myRGB.To <Lab>();

                    Bitplane[2].SetPixel(x, y, myLAB.L);
                    Bitplane[1].SetPixel(x, y, myLAB.A);
                    Bitplane[0].SetPixel(x, y, myLAB.B);
                }
            }
        }
예제 #24
0
        private RubicColors ColorFromHsvColor(int red, int green, int blue)
        {
            var rgb = new Rgb(red, green, blue);

            var hsv = rgb.To <Hsv>();

            var saturation = (int)(hsv.S * 100);

            if (White.IsInRange(saturation))
            {
                return(RubicColors.White);
            }

            if (Yellow.IsInRange((int)hsv.H))
            {
                return(RubicColors.Yellow);
            }
            if (Red.IsInRange((int)hsv.H))
            {
                return(RubicColors.Red);
            }
            if (Orange.IsInRange((int)hsv.H))
            {
                return(RubicColors.Orange);
            }
            if (Blue.IsInRange((int)hsv.H))
            {
                return(RubicColors.Blue);
            }
            if (Green.IsInRange((int)hsv.H))
            {
                return(RubicColors.Green);
            }

            // Debug.Log("Can't find color." + string.Format("h:{0} s:{1} v:{2}", hsv.H, hsv.S, hsv.V));
            #if UNITY_EDITOR
            if (PauseOnColorNotFound)
            {
                UnityEditor.EditorApplication.isPaused = true;
            }
            #endif
            return(RubicColors.Black);
        }
예제 #25
0
        internal void RGBtoXYZ()
        {
            for (int y = 0; y < Height; ++y)
            {
                for (int x = 0; x < Width; ++x)
                {
                    double R = Bitplane[2].GetPixel(x, y);
                    double G = Bitplane[1].GetPixel(x, y);
                    double B = Bitplane[0].GetPixel(x, y);

                    Rgb myRgb = new Rgb(R, G, B);
                    Xyz myXYZ = myRgb.To <Xyz>();

                    Bitplane[2].SetPixel(x, y, myXYZ.X);
                    Bitplane[1].SetPixel(x, y, myXYZ.Y);
                    Bitplane[0].SetPixel(x, y, myXYZ.Z);
                }
            }
        }
예제 #26
0
        public static Color GetIconColor(Bitmap bmp)
        {
            float rtotal = 0;
            float gtotal = 0;
            float btotal = 0;
            float total  = 0;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color c = bmp.GetPixel(x, y);

                    //float saturation = (Math.Max(c.R, Math.Max(c.G, c.B)) - Math.Min(c.R, Math.Min(c.G, c.B))) / 255.0f;
                    float saturation = c.GetSaturation();
                    float relevance  = .1f + .9f * (c.A / 255.0f) * saturation;

                    rtotal += (c.R * relevance);
                    gtotal += (c.G * relevance);
                    btotal += (c.B * relevance);

                    total += relevance * 255;
                }
            }

            Color color = Color.FromArgb(255, (int)(rtotal / total * 255), (int)(gtotal / total * 255), (int)(btotal / total * 255));

            var rgb = new Rgb {
                R = color.R, G = color.G, B = color.B
            };
            var hsv = rgb.To <Hsv>();

            if (hsv.S > 0.15f)
            {
                hsv.S = 0.75f; //values slightly tweaked here from the original
            }
            hsv.V = 0.8f;
            rgb   = hsv.To <Rgb>();

            return(Color.FromArgb(255, (int)rgb.R, (int)rgb.G, (int)rgb.B));
        }
        public static void Get(Color color, ColorPickerMode colorMode, out Point cubeXy, out double columnY)
        {
            var r = color.R;
            var g = color.G;
            var b = color.B;

            double h = 0.0, s = 0.0, v = 0.0;

            if ((int)colorMode < 3)
            {
                var rgb = new Rgb
                {
                    R = r,
                    G = g,
                    B = b
                };
                var hsb = rgb.To <Hsb>();
                h = hsb.H;
                s = hsb.S;
                v = hsb.B;
            }

            Count(colorMode, h, s, v, r, g, b, out cubeXy, out columnY);
        }
예제 #28
0
        public void ProcessStateChanges(SetLampDataExtended data)
        {
            if (data.OnChanged)
            {
                On = Convert.ToBoolean(data.On);
            }
            if (data.ColorChanged)
            {
                if (data.Red >= 0 && data.Red <= 255)
                {
                    Red = Convert.ToByte(data.Red);
                }
                if (data.Green >= 0 && data.Green <= 255)
                {
                    Green = Convert.ToByte(data.Green);
                }
                if (data.Blue >= 0 && data.Blue <= 255)
                {
                    Blue = Convert.ToByte(data.Blue);
                }
            }
            if (data.BrightnessChanged)
            {
                var rgb = new Rgb {
                    R = Red, G = Green, B = Blue
                };
                var hsb = rgb.To <Hsb>();

                hsb.B = data.Brightness;
                rgb   = hsb.To <Rgb>();

                Red   = Convert.ToByte(rgb.R);
                Green = Convert.ToByte(rgb.G);
                Blue  = Convert.ToByte(rgb.B);
            }
        }
예제 #29
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            isRunning       = true;
            backgroundColor = ((SolidColorBrush)FindResource("Background")).Color;
            new HotKey(Key.C, KeyModifier.Ctrl, CopyToClipboard);

            new Thread(() =>
            {
                while (isRunning)
                {
                    Point p = new Point();
                    GetCursorPos(ref p);

                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        colorUnderCursor = GetColorAt(p.X, p.Y);
                        var myRgb        = new Rgb
                        {
                            R = colorUnderCursor.R,
                            G = colorUnderCursor.G,
                            B = colorUnderCursor.B
                        };
                        var lch             = myRgb.To <Lch>();
                        SelectedColor.Fill  = new SolidColorBrush(colorUnderCursor);
                        HelpText.Visibility = colorUnderCursor == backgroundColor ?
                                              Visibility.Visible :
                                              Visibility.Hidden;
                        //Hue.Text = $"{lch.H:0.##}";
                        //Chroma.Text = $"{lch.C:0.##}";
                        //Luminance.Text = $"{lch.L:0.##}";
                    }));

                    Thread.Sleep(100);
                }
            }).Start();
        }
예제 #30
0
        // TODO: FOr colors, money usw.: only one range for all attributes
        private static void GetSettingsFromItems(ItemMapperSettings settings, IEnumerable <LostAndFoundIndexedItem> items)
        {
            var colorConverter = new ColorConverter();

            foreach (var item in items)
            {
                if (settings.OldestDate > item.DateOfIncident)
                {
                    settings.OldestDate = item.DateOfIncident;
                }
                if (settings.NewestDate < item.DateOfIncident)
                {
                    settings.OldestDate = item.DateOfIncident;
                }

                foreach (var attribute in item.Attributes)
                {
                    if (attribute is ColorValueAttribute)
                    {
                        var convertFromInvariantString = colorConverter.ConvertFromInvariantString(attribute.GetValue().ToString()) as Color?;
                        if (convertFromInvariantString == null)
                        {
                            continue;
                        }

                        var colorObject = convertFromInvariantString.Value;
                        var rgb         = new Rgb {
                            R = colorObject.R, G = colorObject.G, B = colorObject.B
                        };
                        var lab = rgb.To <Lab>();

                        IColorAttributeMapperSettings colorAttributeSettings;
                        if (settings.ColorAttributes.ContainsKey(attribute.ID))
                        {
                            colorAttributeSettings = settings.ColorAttributes[attribute.ID];
                        }
                        else
                        {
                            colorAttributeSettings = new ColorAttributeMapperSettings();
                            settings.ColorAttributes.Add(attribute.ID, colorAttributeSettings);
                        }

                        if (lab.L < colorAttributeSettings.LuminescenceSettings.MinValue)
                        {
                            colorAttributeSettings.LuminescenceSettings.MinValue = lab.L;
                        }
                        if (lab.L > colorAttributeSettings.LuminescenceSettings.MaxValue)
                        {
                            colorAttributeSettings.LuminescenceSettings.MaxValue = lab.L;
                        }
                        if (lab.A < colorAttributeSettings.ASettings.MinValue)
                        {
                            colorAttributeSettings.ASettings.MinValue = lab.A;
                        }
                        if (lab.A > colorAttributeSettings.ASettings.MaxValue)
                        {
                            colorAttributeSettings.ASettings.MaxValue = lab.A;
                        }
                        if (lab.B < colorAttributeSettings.BSettings.MinValue)
                        {
                            colorAttributeSettings.BSettings.MinValue = lab.B;
                        }
                        if (lab.B > colorAttributeSettings.BSettings.MaxValue)
                        {
                            colorAttributeSettings.BSettings.MaxValue = lab.B;
                        }

                        continue;
                    }

                    IAttributeMapperSettings attributeSettings;
                    if (settings.Attributes.ContainsKey(attribute.ID))
                    {
                        attributeSettings = settings.Attributes[attribute.ID];
                    }
                    else
                    {
                        attributeSettings = new AttributeMapperSettings();
                        settings.Attributes.Add(attribute.ID, attributeSettings);
                    }
                    attributeSettings.DataCount++;

                    var value = attribute.GetValue();

                    if (value is string)
                    {
                        continue;
                    }

                    var moneyValue = value as MoneyValue;
                    if (moneyValue != null)
                    {
                        if (Convert.ToDouble(moneyValue.Value) < attributeSettings.MinValue)
                        {
                            attributeSettings.MinValue = Convert.ToDouble(moneyValue.Value);
                        }
                        if (Convert.ToDouble(moneyValue.Value) > attributeSettings.MaxValue)
                        {
                            attributeSettings.MaxValue = Convert.ToDouble(moneyValue.Value);
                        }
                        continue;
                    }

                    var convertible = value as IConvertible;
                    if (convertible == null)
                    {
                        continue;
                    }

                    var doubleValue = convertible.ToDouble(CultureInfo.InvariantCulture);
                    if (doubleValue < attributeSettings.MinValue)
                    {
                        attributeSettings.MinValue = doubleValue;
                    }
                    if (doubleValue > attributeSettings.MaxValue)
                    {
                        attributeSettings.MaxValue = doubleValue;
                    }
                }
            }

            settings.DateRange = settings.NewestDate - settings.OldestDate;
        }