LabColor RgbToLab(PaletteEntry cur) { LabColor lab = RgbToLab(cur.R, cur.G, cur.B); lab.Block = cur.Raw; return(lab); }
// Conversion from RGB to CIELAB, using illuminant D65. static LabColor RgbToLab(RgbColor col, bool adjustContrast) { // RGB are assumed to be in [0...255] range double R = col.R / 255d, G = col.G / 255d, B = col.B / 255d; // CIEXYZ coordinates are normalized to [0...1] double x = 0.4124564 * R + 0.3575761 * G + 0.1804375 * B; double y = 0.2126729 * R + 0.7151522 * G + 0.0721750 * B; double z = 0.0193339 * R + 0.1191920 * G + 0.9503041 * B; x /= XN; y /= YN; z /= ZN; x = XyzToLab(x); y = XyzToLab(y); z = XyzToLab(z); LabColor result = new LabColor { // L is normalized to [0...100] L = 116 * y - 16, a = 500 * (x - y), b = 200 * (y - z) }; if (adjustContrast) { result.L *= .75; } return(result); }
public Block[] FindBestMatch(RgbColor color) { if (color.A < TransparencyThreshold * 255) { return(transparent); } LabColor pixelColor = RgbToLab(color, true); double closestDistance = double.MaxValue; Block[] bestMatch = null; foreach (var pair in palette) { double distance = ColorDifference(pixelColor, pair.Key); if (distance < closestDistance) { bestMatch = pair.Value; closestDistance = distance; } } if (bestMatch == null) { throw new Exception("Could not find match: palette is empty!"); } return(bestMatch); }
// http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CMC.html private double CmcDistance(LabColor lab1, LabColor lab2, int l = 2, int c = 1) { var L1 = lab1.L; var L2 = lab2.L; var a1 = lab1.A; var a2 = lab2.A; var b1 = lab1.B; var b2 = lab2.B; var C1 = Math.Sqrt(a1 * a1 + b1 * b1); var C2 = Math.Sqrt(a2 * a2 + b2 * b2); var deltaC = C1 - C2; var deltaL = L1 - L2; var deltaA = a1 - a2; var deltaB = b1 - b2; var deltaH = Math.Sqrt(Math.Pow(deltaA, 2) + Math.Pow(deltaB, 2) + Math.Pow(deltaC, 2)); var H1 = Math.Atan2(b1, a1) * (180 / Math.PI); while (H1 < 0) { H1 += 360; } var F = Math.Sqrt(Math.Pow(C1, 4) / (Math.Pow(C1, 4) + 1900)); var T = (164 <= H1 && H1 <= 345) ? (0.56 + Math.Abs(0.2 * Math.Cos(H1 + 168))) : (0.36 + Math.Abs(0.4 * Math.Cos(H1 + 35))); var S_L = (lab1.L < 16) ? (0.511) : (0.040975 * L1 / (1 + 0.01765 * L1)); var S_C = (0.0638 * C1 / (1 + 0.0131 * C1)) + 0.638; var S_H = S_C * (F * T + 1 - F); var result = Math.Sqrt(Math.Pow(deltaL / (l * S_L), 2) + Math.Pow(deltaC / (c * S_C), 2) + Math.Pow(deltaH / S_H, 2)); return result; }
private static void RgbToLabWithoutColorManagement() { RgbColor rgbColor = new RgbColor(223, 210, 30); LabColor labColor = new LabColor(rgbColor); Console.WriteLine("Without color management: {0} to {1}", rgbColor, labColor); }
public void LabColor() { var first = new LabColor(10, 20.5, 45.445); var second = new LabColor(10, 20.5, 45.445); Assert.Equal(first, (object)second); }
public static Color GetClosestColor(LabColor lc) { //ColorPair select = (ColorPair)(Colors.OrderBy(x => DeltaE.Distance(x.LAB, lc)).ToList().First()); ColorPair select = Colors.MinBy(x => DeltaE.Distance(x.LAB, lc)); return(select.Color); }
private double CompromiseDistance(LabColor lab1, LabColor lab2) { var distances = new List<double>() { CmcDistance(lab1, lab2) }; var coeffs = new List<int>() { 1000 }; var types = new Dictionary<ColorDistanceType, int> { { ColorDistanceType.Protanope, 100 }, { ColorDistanceType.Deuteranope, 500}, { ColorDistanceType.Tritanope, 1} }; foreach (var type in types) { var lab1Cb = Simulate(lab1, type.Key); var lab2Cb = Simulate(lab2, type.Key); if (!(lab1Cb.HasNaN() || lab2Cb.HasNaN())) { distances.Add(CmcDistance(lab1Cb, lab2Cb)); coeffs.Add(type.Value); } } var total = 0.0; var count = 0.0; for (var i = 0; i < distances.Count; i++) { total += coeffs[i] * distances[i]; count += coeffs[i]; } return total / count; }
public SalientPixel(LabColor color, int x, int y, int numberOfPatches) { Color = color; X = x; Y = y; Patches = new Patch[numberOfPatches]; }
public void Equals_Same() { var first = new LabColor(l: 10, a: 20.5, b: 45.445); var second = new LabColor(l: 10, a: 20.5, b: 45.445); CustomAssert.EqualsWithHashCode(first, second); }
private double EuclidianDistance(LabColor lab1, LabColor lab2) { var dL = lab1.L - lab2.L; var dA = lab1.A - lab2.A; var dB = lab1.B - lab2.B; return Math.Sqrt(dL * dL + dA * dA + dB * dB); }
public void Equals_Different() { var first = new LabColor(l: 11, a: 20.5, b: 45.445); var second = new LabColor(l: 10, a: 20.5, b: 45.445); CustomAssert.NotEqualsWithHashCode(first, second); }
private Color GetNewColor(byte r, byte g, byte b) { var labColor = LabColor.FromRgb(r, g, b); return(_black.DistanceTo(labColor) < ToleranceDistance? Color.FromRgb(0, 0, 0) : Color.FromRgb(255, 255, 255)); }
// Conversion from RGB to CIELAB, using illuminant D65. static LabColor RgbToLab(RgbColor color, bool adjustContrast) { // RGB are assumed to be in [0...255] range double R = color.R / 255d; double G = color.G / 255d; double B = color.B / 255d; // CIEXYZ coordinates are normalized to [0...1] double x = 0.4124564 * R + 0.3575761 * G + 0.1804375 * B; double y = 0.2126729 * R + 0.7151522 * G + 0.0721750 * B; double z = 0.0193339 * R + 0.1191920 * G + 0.9503041 * B; double xRatio = x / XN; double yRatio = y / YN; double zRatio = z / ZN; LabColor result = new LabColor { // L is normalized to [0...100] L = 116 * XyzToLab(yRatio) - 16, a = 500 * (XyzToLab(xRatio) - XyzToLab(yRatio)), b = 200 * (XyzToLab(yRatio) - XyzToLab(zRatio)) }; if (adjustContrast) { result.L *= .75; } return(result); }
// CIE76 formula for Delta-E, over CIELAB color space static double ColorDifference(LabColor color1, LabColor color2) { return (Math.Sqrt((color2.L - color1.L) * (color2.L - color1.L) * 1.2 + (color2.a - color1.a) * (color2.a - color1.a) + (color2.b - color1.b) * (color2.b - color1.b))); }
private static void LabToCmykWithColorManagement() { LabColor labColor = new LabColor(179, -20, 29, 255); CmykColor cmykColor = labColor.To <CmykColor>("../../../../_Input/ColorProfiles/ISOcoated_v2_eci.icc"); Console.WriteLine("With color management: {0} to {1}", labColor, cmykColor); }
private static double DColor(LabColor c1, LabColor c2) { return(Math.Sqrt( Math.Pow((c1.a - c2.a), 2) + Math.Pow((c1.b - c2.b), 2) + Math.Pow((c1.L - c2.L), 2) )); }
private double DistanceColorBlind(LabColor lab1, LabColor lab2, ColorDistanceType type) { var lab1Cb = Simulate(lab1, type); var lab2Cb = Simulate(lab2, type); return CmcDistance(lab1Cb, lab2Cb); }
public static List <Color> GetPalette(int colorCount, PaletteOptions options = null) { if (colorCount < 1) { return(new List <Color>()); } if (options == null) { options = new PaletteOptions(); } if (options.Samples < colorCount * 5) { options.Samples = colorCount * 5; } ; var samples = GenerateSamples(options); if (samples.Count < colorCount) { throw new Exception("Not enough samples to generate palette, increase sample count."); } var sliceSize = samples.Count / colorCount; var colors = new List <LabColor>(); for (var i = 0; i < samples.Count; i += sliceSize) { colors.Add(samples[i]); if (colors.Count >= colorCount) { break; } } for (var step = 1; step <= options.Quality; step++) { var zones = GenerateZones(samples, colors); var lastColors = colors.Select(x => x).ToList(); for (var i = 0; i < zones.Count; i++) { var zone = zones[i]; var total = zone.Count; var lAvg = zone.Sum(x => x.L) / total; var aAvg = zone.Sum(x => x.A) / total; var bAvg = zone.Sum(x => x.B) / total; colors[i] = new LabColor(lAvg, aAvg, bAvg); } if (!AreEqualPalettes(lastColors, colors)) { break; } } colors = SortByContrast(colors); return(colors.Select((lab) => lab.ToRgb()).ToList()); }
public void VectorCtor() { var first = new LabColor(l: 10, a: 20.5, b: 45.445); var vector = new[] { 10, 20.5, 45.445 }; var second = new LabColor(vector); CustomAssert.EqualsWithHashCode(first, second); Assert.Equal(vector, second.Vector); }
private ConfigSingleton() { _pointsPerUnit = 100; _centerPixelPosition = new Vector2((float)Screen.width / 2, (float)Screen.height / 2); SetCameraProperties(DisplayMode.ConstantUnitSize); _rng = new System.Random(); _testCaseList = new List <TestCase>(); _skyboxColor = Helpers.Color2Lab(Camera.main.backgroundColor); }
protected void Add(LabColor color, Block[] blocks) { if (blocks == null) { throw new ArgumentNullException("blocks"); } if (blocks.Length != Layers) { throw new ArgumentException("Number of blocks must match number the of layers."); } palette.Add(color, blocks); }
public static LabColor Color2Lab(Color color) { var converter = new ColourfulConverter { WhitePoint = Illuminants.D50, TargetRGBWorkingSpace = RGBWorkingSpaces.sRGB }; RGBColor rgbColor = new RGBColor(color.r, color.g, color.b); LabColor output = converter.ToLab(rgbColor); return(output); }
/// <summary> /// Chromatic adaptation /// The adaptation can be also performed alone (e.g. from CIELAB D50 to CIELAB D65). /// </summary> private void ChromaticAdaptation() { LabColor input = new LabColor(10, 20, 30, Illuminants.D50); var converter = new ColourfulConverter { TargetLabWhitePoint = Illuminants.D65 }; LabColor output = converter.Adapt(input); } // end ChromaticAdaptation
public static double LabDiff(LabColor c1, LabColor c2) { // https://en.wikipedia.org/wiki/Color_difference#CIE76 double dL = c1.L - c2.L; double da = c1.a - c2.a; double db = c1.b - c2.b; double sqareSum = Math.Pow(dL, 2) + Math.Pow(da, 2) + Math.Pow(db, 2); return(Math.Sqrt(sqareSum)); }
private async Task <DrinkStats> GetDrinkStats() { /** http://www.colorwiki.com/wiki/Delta_E:_The_Color_Difference */ float promR = 0, promG = 0, promB = 0; HashSet <Color> differentColors = new HashSet <Color>(); DynamicParticle[] allObjects = (DynamicParticle[])FindObjectsOfType(typeof(DynamicParticle)); int count = 0; int quantity = allObjects.Length; if (quantity < 10) { return(null); } foreach (DynamicParticle obj in allObjects) { count++; Color color = obj.currentImage.gameObject.GetComponent <MeshRenderer>().materials[0].GetColor("_Color"); promR += color.r; promG += color.g; promB += color.b; differentColors.Add(color); } promR = promR / allObjects.Length; promG = promG / allObjects.Length; promB = promB / allObjects.Length; Color promColor = new Color(promR, promG, promB); foreach (DynamicParticle obj in allObjects) { //Destroy(obj.gameObject); obj.SetColor(promColor); await Task.Delay(10); /** Simula que se toma la wea*/ } var color1 = new LabColor(promR, promG, promB); var color2 = new LabColor(customer.wantedColor.r, customer.wantedColor.g, customer.wantedColor.b); double deltaE = new CIEDE2000ColorDifference().ComputeDifference(color1, color2); drinkStats = new DrinkStats() { preparationTime = Time.time - initialTime, mixture = (differentColors.Count * 100) / quantity, colorSimilarity = (float)deltaE, quantity = quantity, promColor = promColor }; return(drinkStats); }
private void ReadColors(BinaryReader reader, Swatch swatch, int count, short version) { for (int i = 0; i < count; i++) { var space = (ColorSpace)reader.ReadInt16(); var item1 = reader.ReadUInt16(); var item2 = reader.ReadUInt16(); var item3 = reader.ReadUInt16(); var item4 = reader.ReadUInt16(); ColorBase color = null; switch (space) { case ColorSpace.RGB: color = new RgbColor(Convert.ToByte(item1 / 256), Convert.ToByte(item2 / 256), Convert.ToByte(item3 / 256)); break; case ColorSpace.HSB: color = new HsvColor(item1 / 65535f, item2 / 65535f, item3 / 65535f); break; case ColorSpace.Grayscale: color = new GrayColor(item1 / 10000f); break; case ColorSpace.CMYK: color = new CmykColor(1f - (item1 / 65535f), 1f - (item2 / 65535f), 1f - (item3 / 65535f), 1f - (item4 / 65535f)); break; case ColorSpace.WideCMYK: color = new CmykColor(item1 / 10000f, item2 / 10000f, item3 / 10000f, item4 / 10000f); break; case ColorSpace.Lab: color = new LabColor((short)(item1 / 100), (short)(item2 / 100), (short)(item3 / 100)); break; default: break; } if (version == 2) { reader.ReadBytes(2); var nameLength = reader.ReadInt16() * 2; if (nameLength > 0) { var nameBuffer = reader.ReadBytes(nameLength); color.Name = Encoding.BigEndianUnicode.GetString(nameBuffer, 0, nameLength - 2); } } swatch.Colors.Add(color); } }
public void Dctor() { const double l1 = 10; const double a1 = 20.5; const double b1 = 45.445; var(l2, a2, b2) = new LabColor(l1, a1, b1); Assert.Equal(l1, l2); Assert.Equal(a1, a2); Assert.Equal(b1, b2); }
private static void RgbToLabWithColorManagement() { RgbColor rgbColor = new RgbColor(223, 210, 30) { Profile = ColorProfile.FromSrgb() }; LabColor labColor = rgbColor.To <LabColor>(); Console.WriteLine("With color management: {0} to {1}", rgbColor, labColor); }
public Color Darken(double amount = 1) { LabColor lab = this.Lab(); LabColor lab2 = new LabColor(lab.L - (LabConsts.Kn * amount), lab.a, lab.b); using (ColorToolExtensions.BorrowColourfulConverter(out var conv)) { RGBColor rgb = conv.ToRGB(lab2); return(Alpha(rgb, Alpha(), true)); } }
public bool ValidateColor(LabColor color) { var hcl = color.ToHcl(); return((hcl.H >= this.HueMin) && (hcl.H <= this.HueMin) && (hcl.C >= this.ChromaMin) && (hcl.C <= this.ChromaMax) && (hcl.L >= this.LightMin) && (hcl.L <= this.LightMax)); }
public void CIE94ColorDifference_GraphicArts(double l1, double a1, double b1, double l2, double a2, double b2, double expectedDeltaE) { // arrange var x = new LabColor(l1, a1, b1); var y = new LabColor(l2, a2, b2); // act double deltaE = new CIE94ColorDifference(CIE94ColorDifferenceApplication.GraphicArts).ComputeDifference(x, y); // assert Assert.That(deltaE, Is.EqualTo(expectedDeltaE).Using(DoubleComparerLabPrecision)); }
public void Convert_Lab_to_LCHab(double l, double a, double b, double l2, double c, double h) { // arrange var input = new LabColor(l, a, b); // act LChabColor output = Converter.ToLChab(input); // assert Assert.That(output.L, Is.EqualTo(l2).Using(DoubleComparer)); Assert.That(output.C, Is.EqualTo(c).Using(DoubleComparer)); Assert.That(output.h, Is.EqualTo(h).Using(DoubleComparer)); }
public void Convert_Lab_to_XYZ(double l, double a, double b, double x, double y, double z) { // arrange var input = new LabColor(l, a, b, Illuminants.D65); var converter = new ColourfulConverter { WhitePoint = Illuminants.D65, TargetLabWhitePoint = Illuminants.D65 }; // act XYZColor output = converter.ToXYZ(input); // assert Assert.That(output.X, Is.EqualTo(x).Using(DoubleComparerXYZPrecision)); Assert.That(output.Y, Is.EqualTo(y).Using(DoubleComparerXYZPrecision)); Assert.That(output.Z, Is.EqualTo(z).Using(DoubleComparerXYZPrecision)); }
/// <summary> /// Adapts Lab color from the source white point to white point set in <see cref="TargetLabWhitePoint"/>. /// </summary> public LabColor Adapt(LabColor color) { if (color == null) throw new ArgumentNullException("color"); if (!IsChromaticAdaptationPerformed) throw new InvalidOperationException("Cannot perform chromatic adaptation, provide chromatic adaptation method and white point."); if (color.WhitePoint.Equals(TargetLabWhitePoint)) return color; XYZColor xyzColor = ToXYZ(color); LabColor result = ToLab(xyzColor); return result; }
public void Adapt_Lab_D65_To_D50(double l1, double a1, double b1, double l2, double a2, double b2) { // arrange var input = new LabColor(l1, a1, b1, Illuminants.D65); var expectedOutput = new LabColor(l2, a2, b2); var converter = new ColourfulConverter { TargetLabWhitePoint = Illuminants.D50 }; // action LabColor output = converter.Adapt(input); // assert Assert.That(output.L, Is.EqualTo(expectedOutput.L).Using(DoublePrecisionComparer)); Assert.That(output.a, Is.EqualTo(expectedOutput.a).Using(DoublePrecisionComparer)); Assert.That(output.b, Is.EqualTo(expectedOutput.b).Using(DoublePrecisionComparer)); }
public void LabColor() { var color = new LabColor(10, 20.5, 45.445); Assert.AreEqual("Lab [L=10, a=20.5, b=45.45]", color.ToString()); }
// CIE76 formula for Delta-E, over CIELAB color space static double ColorDifference(LabColor color1, LabColor color2) { return Math.Sqrt((color2.L - color1.L) * (color2.L - color1.L) * 1.2 + (color2.a - color1.a) * (color2.a - color1.a) + (color2.b - color1.b) * (color2.b - color1.b)); }
// Conversion from RGB to CIELAB, using illuminant D65. static LabColor RgbToLab(RgbColor color, bool adjustContrast) { // RGB are assumed to be in [0...255] range double R = color.R / 255d; double G = color.G / 255d; double B = color.B / 255d; // CIEXYZ coordinates are normalized to [0...1] double x = 0.4124564 * R + 0.3575761 * G + 0.1804375 * B; double y = 0.2126729 * R + 0.7151522 * G + 0.0721750 * B; double z = 0.0193339 * R + 0.1191920 * G + 0.9503041 * B; double xRatio = x / XN; double yRatio = y / YN; double zRatio = z / ZN; LabColor result = new LabColor { // L is normalized to [0...100] L = 116 * XyzToLab(yRatio) - 16, a = 500 * (XyzToLab(xRatio) - XyzToLab(yRatio)), b = 200 * (XyzToLab(yRatio) - XyzToLab(zRatio)) }; if (adjustContrast) { result.L *= .75; } return result; }
public void CIEDE2000ColorDifference(double l1, double a1, double b1, double l2, double a2, double b2, double expectedDeltaE) { // arrange var x = new LabColor(l1, a1, b1); var y = new LabColor(l2, a2, b2); // act double deltaE = new CIEDE2000ColorDifference().ComputeDifference(x, y); // assert Assert.That(deltaE, Is.EqualTo(expectedDeltaE).Using(DoubleComparerLabRounding)); }
public void CMCColorDifference(double l1, double a1, double b1, double l2, double a2, double b2, double expectedDeltaE_imperceptibility, double expectedDeltaE_acceptability) { // arrange var x = new LabColor(l1, a1, b1); var y = new LabColor(l2, a2, b2); // act double deltaE_imperceptibility = new CMCColorDifference(CMCColorDifferenceThreshold.Imperceptibility).ComputeDifference(x, y); double deltaE_acceptability = new CMCColorDifference(CMCColorDifferenceThreshold.Acceptability).ComputeDifference(x, y); // assert Assert.That(deltaE_imperceptibility, Is.EqualTo(expectedDeltaE_imperceptibility).Using(DoubleComparerLabPrecision)); Assert.That(deltaE_acceptability, Is.EqualTo(expectedDeltaE_acceptability).Using(DoubleComparerLabPrecision)); }