/// <summary> /// Read all color spaces defined the resources /// </summary> private void ReadResourceDefinedColorSpaces(PdfResources resourcesDict) { var colorSpaceDict = resourcesDict.ColorSpaces; if (colorSpaceDict == null) { return; } foreach (var name in colorSpaceDict.Elements.Keys) { var colorSpaceArray = colorSpaceDict.Elements.GetArray(name); var type = colorSpaceArray.Elements.GetName(0); IColorSpace colorSpace = null; switch (type) { case PdfKeys.DevicePattern: { colorSpace = new PatternColorSpace(patternManager); colorSpace.Init(colorSpaceArray); break; } default: colorSpace = CreateColorSpace(colorSpaceArray); break; } colorSpaces.Add(name, colorSpace); } }
public double Compare(IColorSpace colorA, IColorSpace colorB) { var aLab = colorA.To <Lab>(); var bLab = colorB.To <Lab>(); var deltaL = aLab.L - bLab.L; var h = Math.Atan2(aLab.B, aLab.A); var c1 = Math.Sqrt(Math.Pow(aLab.A, 2) + Math.Pow(aLab.B, 2)); var c2 = Math.Sqrt(Math.Pow(bLab.A, 2) + Math.Pow(bLab.B, 2)); var deltaC = c1 - c2; var deltaH = Math.Sqrt(Math.Pow(aLab.A - bLab.A, 2) + Math.Pow(aLab.B - bLab.B, 2) - Math.Pow(deltaC, 2)); var t = 164 <= h || h >= 345 ? .56 + Math.Abs(.2 * Math.Cos(h + 168.0)) : .36 + Math.Abs(.4 * Math.Cos(h + 35.0)); var f = Math.Sqrt(Math.Pow(c1, 4) / (Math.Pow(c1, 4) + 1900.0)); var sL = aLab.L < 16 ? .511 : (.040975 * aLab.L) / (1.0 + .01765 * aLab.L); var sC = (.0638 * c1) / (1 + .0131 * c1) + .638; var sH = sC * (f * t + 1 - f); var differences = DistanceDivided(deltaL, _lightness * sL) + DistanceDivided(deltaC, _chroma * sC) + DistanceDivided(deltaH, sH); return(Math.Sqrt(differences)); }
internal ColorChannelDefinition(IColorSpace colorSpace, string channelName, string shortChannelName, Func<double, double> valueToDisplayValueFunc) { this.colorSpace = colorSpace; this.name = channelName; this.shortName = shortChannelName; this.valueToDisplayValueFunc = valueToDisplayValueFunc; }
/// <summary> /// Init /// </summary> public void Init(PdfArray colorSpaceArray) { var baseArray = colorSpaceArray.Elements.GetArray(1); if (baseArray != null) { baseColorSpace = ColorSpaceManager.CreateColorSpace(baseArray); } else { string baseColorSpaceName = colorSpaceArray.Elements.GetName(1); baseColorSpace = ColorSpaceManager.CreateColorSpace(baseColorSpaceName); } hival = colorSpaceArray.Elements.GetInteger(2); if (colorSpaceArray.Elements[3] is PdfString hexString) { lookup = new List <double>(); for (int i = 0; i < hexString.Value.Length; i++) { lookup.Add(hexString.Value[i] / 255.0); } } }
/// <summary> /// Compare colors using the Cie94 algorithm. The first color (a) will be used as the reference color. /// </summary> /// <param name="a">Reference color</param> /// <param name="b">Comparison color</param> /// <returns></returns> public double Compare(IColorSpace a, IColorSpace b) { var labA = a.To<Lab>(); var labB = b.To<Lab>(); var deltaL = labA.L - labB.L; var deltaA = labA.A - labB.A; var deltaB = labA.B - labB.B; var c1 = Math.Sqrt(labA.A * labA.A + labA.B * labA.B); var c2 = Math.Sqrt(labB.A * labB.A + labB.B * labB.B); var deltaC = c1 - c2; var deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC; deltaH = deltaH < 0 ? 0 : Math.Sqrt(deltaH); const double sl = 1.0; const double kc = 1.0; const double kh = 1.0; var sc = 1.0 + Constants.K1 * c1; var sh = 1.0 + Constants.K2 * c1; var deltaLKlsl = deltaL / (Constants.Kl * sl); var deltaCkcsc = deltaC / (kc * sc); var deltaHkhsh = deltaH / (kh * sh); var i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh; return i < 0 ? 0 : Math.Sqrt(i); }
private void ReturnsExpectedValueForKnownInput(double expectedValue, IColorSpace a, IColorSpace b) { var target = new Cie1976Comparison(); var actualValue = a.Compare(b, target); Assert.IsTrue(expectedValue.BasicallyEqualTo(actualValue)); }
/// <summary> /// Init /// </summary> public void Init(EpsInterpreter interpreter, ArrayOperand colorSpaceDetails) { this.interpreter = interpreter; this.colorSpaceDetails = colorSpaceDetails; switch (colorSpaceDetails.Values[1].Operand) { case StringOperand stringOperand: case NameOperand nameOperand: numberOfValuesPerColor = 1; break; case ArrayOperand namesArray: numberOfValuesPerColor = namesArray.Values.Count; break; default: throw new Exception("illegal type"); } // we only operate on the alternative color space var colorSpaceOp = colorSpaceDetails.Values[2].Operand; alternativeColorSpace = ColorSpaceActivator.CreateColorSpace(interpreter, colorSpaceOp); tintTransformationProcedure = (ArrayOperand)colorSpaceDetails.Values[3].Operand; }
/// <summary> /// Compare colors using the Cie94 algorithm. The first color (a) will be used as the reference color. /// </summary> /// <param name="a">Reference color</param> /// <param name="b">Comparison color</param> /// <returns></returns> public double Compare(IColorSpace a, IColorSpace b) { var labA = a.To <Lab>(); var labB = b.To <Lab>(); var deltaL = labA.L - labB.L; var deltaA = labA.A - labB.A; var deltaB = labA.B - labB.B; var c1 = Math.Sqrt(labA.A * labA.A + labA.B * labA.B); var c2 = Math.Sqrt(labB.A * labB.A + labB.B * labB.B); var deltaC = c1 - c2; var deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC; deltaH = deltaH < 0 ? 0 : Math.Sqrt(deltaH); const double sl = 1.0; const double kc = 1.0; const double kh = 1.0; var sc = 1.0 + Constants.K1 * c1; var sh = 1.0 + Constants.K2 * c1; var deltaLKlsl = deltaL / (Constants.Kl * sl); var deltaCkcsc = deltaC / (kc * sc); var deltaHkhsh = deltaH / (kh * sh); var i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh; return(i < 0 ? 0 : Math.Sqrt(i)); }
public static ColorRGB WithAlpha(this IColorSpace color, double amount) { var rgb = (ColorRGB)color.ToRgb(); rgb.A = amount; return(rgb); }
public double Compare(IColorSpace colorA, IColorSpace colorB) { var aLab = colorA.To<Lab>(); var bLab = colorB.To<Lab>(); var deltaL = aLab.L - bLab.L; var h = Math.Atan2(aLab.B,aLab.A); var c1 = Math.Sqrt(Math.Pow(aLab.A, 2) + Math.Pow(aLab.B, 2)); var c2 = Math.Sqrt(Math.Pow(bLab.A, 2) + Math.Pow(bLab.B, 2)); var deltaC = c1 - c2; var deltaH = Math.Sqrt(Math.Pow(aLab.A - bLab.A, 2) + Math.Pow(aLab.B - bLab.B, 2) - Math.Pow(deltaC,2)); var t = 164 <= h || h >= 345 ? .56 + Math.Abs(.2*Math.Cos(h + 168.0)) : .36 + Math.Abs(.4*Math.Cos(h + 35.0)); var f = Math.Sqrt(Math.Pow(c1,4)/(Math.Pow(c1,4) + 1900.0)); var sL = aLab.L < 16 ? .511 : (.040975 * aLab.L) / (1.0 + .01765 * aLab.L); var sC = (.0638 * c1) / (1 + .0131 * c1) + .638; var sH = sC*(f*t + 1 - f); var differences = DistanceDivided(deltaL, _lightness * sL) + DistanceDivided(deltaC, _chroma * sC) + DistanceDivided(deltaH, sH); return Math.Sqrt(differences); }
/// <summary> /// Calculates the CMC l:c (1984) delta-e value: http://en.wikipedia.org/wiki/Color_difference#CMC_l:c_.281984.29 /// </summary> /// <param name="colorA"></param> /// <param name="colorB"></param> /// <returns></returns> public double Compare(IColorSpace colorA, IColorSpace colorB) { var aLab = colorA.To<Lab>(); var bLab = colorB.To<Lab>(); var deltaL = aLab.L - bLab.L; var h = Math.Atan2(aLab.B, aLab.A); var c1 = Math.Sqrt(aLab.A * aLab.A + aLab.B * aLab.B); var c2 = Math.Sqrt(bLab.A * bLab.A + bLab.B * bLab.B); var deltaC = c1 - c2; var deltaH = Math.Sqrt( (aLab.A - bLab.A) * (aLab.A - bLab.A) + (aLab.B - bLab.B) * (aLab.B - bLab.B) - deltaC * deltaC); var c1_4 = c1 * c1; c1_4 *= c1_4; var t = 164 <= h && h <= 345 ? .56 + Math.Abs(.2 * Math.Cos(h + 168.0)) : .36 + Math.Abs(.4 * Math.Cos(h + 35.0)); var f = Math.Sqrt(c1_4 / (c1_4 + 1900.0)); var sL = aLab.L < 16 ? .511 : (.040975 * aLab.L) / (1.0 + .01765 * aLab.L); var sC = (.0638 * c1) / (1 + .0131 * c1) + .638; var sH = sC * (f * t + 1 - f); var differences = DistanceDivided(deltaL, _lightness * sL) + DistanceDivided(deltaC, _chroma * sC) + DistanceDivided(deltaH, sH); return Math.Sqrt(differences); }
/// <summary> /// Calculates the CMC l:c (1984) delta-e value: http://en.wikipedia.org/wiki/Color_difference#CMC_l:c_.281984.29 /// </summary> /// <param name="colorA"></param> /// <param name="colorB"></param> /// <returns></returns> public double Compare(IColorSpace colorA, IColorSpace colorB) { var aLab = colorA.To <Lab>(); var bLab = colorB.To <Lab>(); var deltaL = aLab.L - bLab.L; var h = Math.Atan2(aLab.B, aLab.A); var c1 = Math.Sqrt(aLab.A * aLab.A + aLab.B * aLab.B); var c2 = Math.Sqrt(bLab.A * bLab.A + bLab.B * bLab.B); var deltaC = c1 - c2; var deltaH = Math.Sqrt( (aLab.A - bLab.A) * (aLab.A - bLab.A) + (aLab.B - bLab.B) * (aLab.B - bLab.B) - deltaC * deltaC); var c1_4 = c1 * c1; c1_4 *= c1_4; var t = 164 <= h && h <= 345 ? .56 + Math.Abs(.2 * Math.Cos(h + 168.0)) : .36 + Math.Abs(.4 * Math.Cos(h + 35.0)); var f = Math.Sqrt(c1_4 / (c1_4 + 1900.0)); var sL = aLab.L < 16 ? .511 : (.040975 * aLab.L) / (1.0 + .01765 * aLab.L); var sC = (.0638 * c1) / (1 + .0131 * c1) + .638; var sH = sC * (f * t + 1 - f); var differences = DistanceDivided(deltaL, _lightness * sL) + DistanceDivided(deltaC, _chroma * sC) + DistanceDivided(deltaH, sH); return(Math.Sqrt(differences)); }
/// <summary> /// Compares the current <see cref="IColorSpace"/> instance with another <see cref="IColorSpace"/> of the same type and returns an <see cref="int"/> that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. /// </summary> /// <param name="other">A <see cref="IColorSpace"/> instance to compare with this <see cref="IColorSpace"/> instance.</param> /// <returns> /// A value that indicates the relative order of the objects being compared. The return value has these meanings: <list type="table"> /// <listheader> /// <term>Value</term> <description>Description</description> /// </listheader> <item> /// <term>Less than zero</term> <description>This instance precedes <paramref name="other"/> in the sort order.</description> /// </item> <item> /// <term>Zero</term> <description>This instance occurs in the same position in the sort order as <paramref name="other"/>.</description> /// </item> <item> /// <term>Greater than zero</term> <description>This instance follows <paramref name="other"/> in the sort order.</description> /// </item> /// </list> /// </returns> public int CompareTo(IColorSpace other) { var left = this.ToColor(); var right = other.ToColor(); return((left.R + left.G + left.B).CompareTo(right.R + right.G + right.B)); }
/// <summary> /// Compare colors using the Cie94 algorithm. The first color (a) will be used as the reference color. /// </summary> /// <param name="a">Reference color</param> /// <param name="b">Comparison color</param> /// <returns></returns> public double Compare(IColorSpace a, IColorSpace b) { var labA = a.To <Lab>(); var labB = b.To <Lab>(); var deltaL = labA.L - labB.L; var deltaA = labA.A - labB.A; var deltaB = labA.B - labB.B; var c1 = Math.Sqrt(Math.Pow(labA.A, 2) + Math.Pow(labA.B, 2)); var c2 = Math.Sqrt(Math.Pow(labB.A, 2) + Math.Pow(labB.B, 2)); var deltaC = c1 - c2; var deltaH = Math.Pow(deltaA, 2) + Math.Pow(deltaB, 2) - Math.Pow(deltaC, 2); deltaH = deltaH < 0 ? 0 : Math.Sqrt(deltaH); const double sl = 1.0; const double kc = 1.0; const double kh = 1.0; var sc = 1.0 + Constants.K1 * c1; var sh = 1.0 + Constants.K2 * c1; var i = Math.Pow(deltaL / (Constants.Kl * sl), 2) + Math.Pow(deltaC / (kc * sc), 2) + Math.Pow(deltaH / (kh * sh), 2); return(i < 0 ? 0 : Math.Sqrt(i)); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ICmy expectedColor) { var target = knownColor.To <Cmy>(); Assert.IsTrue(CloseEnough(expectedColor.C, target.C), "(C)" + expectedColor.C + " != " + target.C); Assert.IsTrue(CloseEnough(expectedColor.M, target.M), "(M)" + expectedColor.M + " != " + target.M); Assert.IsTrue(CloseEnough(expectedColor.Y, target.Y), "(Y)" + expectedColor.Y + " != " + target.Y); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IYxy expectedColor) { var target = knownColor.To<Yxy>(); Assert.IsTrue(CloseEnough(expectedColor.Y1,target.Y1),"(Y1)" + expectedColor.Y1 + " != " + target.Y1); Assert.IsTrue(CloseEnough(expectedColor.X,target.X),"(X)" + expectedColor.X + " != " + target.X); Assert.IsTrue(CloseEnough(expectedColor.Y2,target.Y2),"(Y2)" + expectedColor.Y2 + " != " + target.Y2); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IRgb expectedColor) { var target = knownColor.To<Rgb>(); Assert.IsTrue(CloseEnough(expectedColor.R,target.R),"(R)" + expectedColor.R + " != " + target.R); Assert.IsTrue(CloseEnough(expectedColor.G,target.G),"(G)" + expectedColor.G + " != " + target.G); Assert.IsTrue(CloseEnough(expectedColor.B,target.B),"(B)" + expectedColor.B + " != " + target.B); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILab expectedColor) { var target = knownColor.To<Lab>(); Assert.IsTrue(CloseEnough(expectedColor.L,target.L),"(L)" + expectedColor.L + " != " + target.L); Assert.IsTrue(CloseEnough(expectedColor.A,target.A),"(A)" + expectedColor.A + " != " + target.A); Assert.IsTrue(CloseEnough(expectedColor.B,target.B),"(B)" + expectedColor.B + " != " + target.B); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ICmy expectedColor) { var target = knownColor.To <Cmy>(); Assert.AreEqual(expectedColor.C, target.C, 0.005, "(C)" + expectedColor.C + " != " + target.C); Assert.AreEqual(expectedColor.M, target.M, 0.005, "(M)" + expectedColor.M + " != " + target.M); Assert.AreEqual(expectedColor.Y, target.Y, 0.005, "(Y)" + expectedColor.Y + " != " + target.Y); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IHsv expectedColor) { var target = knownColor.To <Hsv>(); Assert.IsTrue(CloseEnough(expectedColor.H, target.H), "(H)" + expectedColor.H + " != " + target.H); Assert.IsTrue(CloseEnough(expectedColor.S, target.S), "(S)" + expectedColor.S + " != " + target.S); Assert.IsTrue(CloseEnough(expectedColor.V, target.V), "(V)" + expectedColor.V + " != " + target.V); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IHsv expectedColor) { var target = knownColor.To <Hsv>(); Assert.AreEqual(expectedColor.H, target.H, 1.8, "(H)" + expectedColor.H + " != " + target.H); Assert.AreEqual(expectedColor.S, target.S, 0.005, "(S)" + expectedColor.S + " != " + target.S); Assert.AreEqual(expectedColor.V, target.V, 0.005, "(V)" + expectedColor.V + " != " + target.V); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IYxy expectedColor) { var target = knownColor.To <Yxy>(); Assert.IsTrue(CloseEnough(expectedColor.Y1, target.Y1), "(Y1)" + expectedColor.Y1 + " != " + target.Y1); Assert.IsTrue(CloseEnough(expectedColor.X, target.X), "(X)" + expectedColor.X + " != " + target.X); Assert.IsTrue(CloseEnough(expectedColor.Y2, target.Y2), "(Y2)" + expectedColor.Y2 + " != " + target.Y2); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILuv expectedColor) { var target = knownColor.To <Luv>(); Assert.IsTrue(CloseEnough(expectedColor.L, target.L), "(L)" + expectedColor.L + " != " + target.L); Assert.IsTrue(CloseEnough(expectedColor.U, target.U), "(U)" + expectedColor.U + " != " + target.U); Assert.IsTrue(CloseEnough(expectedColor.V, target.V), "(V)" + expectedColor.V + " != " + target.V); }
public static ColorRGB Saturated(this IColorSpace color, double amount = 0.2) { var hsl = ColorHSL.FromColor(color.ToRgb()); hsl.S += amount; return((ColorRGB)hsl.ToRgb()); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILab expectedColor) { var target = knownColor.To <Lab>(); Assert.AreEqual(expectedColor.L, target.L, 0.5, "(L)" + expectedColor.L + " != " + target.L); Assert.AreEqual(expectedColor.A, target.A, 0.64, "(A)" + expectedColor.A + " != " + target.A); Assert.AreEqual(expectedColor.B, target.B, 0.64, "(B)" + expectedColor.B + " != " + target.B); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IYxy expectedColor) { var target = knownColor.To <Yxy>(); Assert.AreEqual(expectedColor.Y1, target.Y1, 0.5, "(Y1)" + expectedColor.Y1 + " != " + target.Y1); Assert.AreEqual(expectedColor.X, target.X, 0.005, "(X)" + expectedColor.X + " != " + target.X); Assert.AreEqual(expectedColor.Y2, target.Y2, 0.005, "(Y2)" + expectedColor.Y2 + " != " + target.Y2); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IRgb expectedColor) { var target = knownColor.To <Rgb>(); Assert.AreEqual(expectedColor.R, target.R, 1.275, "(R)" + expectedColor.R + " != " + target.R); Assert.AreEqual(expectedColor.G, target.G, 1.275, "(G)" + expectedColor.G + " != " + target.G); Assert.AreEqual(expectedColor.B, target.B, 1.275, "(B)" + expectedColor.B + " != " + target.B); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILuv expectedColor) { var target = knownColor.To <Luv>(); Assert.AreEqual(expectedColor.L, target.L, 0.5, "(L)" + expectedColor.L + " != " + target.L); Assert.AreEqual(expectedColor.U, target.U, 1.12, "(U)" + expectedColor.U + " != " + target.U); Assert.AreEqual(expectedColor.V, target.V, 0.61, "(V)" + expectedColor.V + " != " + target.V); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ICmy expectedColor) { var target = knownColor.To<Cmy>(); Assert.IsTrue(CloseEnough(expectedColor.C,target.C),"(C)" + expectedColor.C + " != " + target.C); Assert.IsTrue(CloseEnough(expectedColor.M,target.M),"(M)" + expectedColor.M + " != " + target.M); Assert.IsTrue(CloseEnough(expectedColor.Y,target.Y),"(Y)" + expectedColor.Y + " != " + target.Y); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IXyz expectedColor) { var target = knownColor.To <Xyz>(); Assert.AreEqual(expectedColor.X, target.X, 0.5, "(X)" + expectedColor.X + " != " + target.X); Assert.AreEqual(expectedColor.Y, target.Y, 0.5, "(Y)" + expectedColor.Y + " != " + target.Y); Assert.AreEqual(expectedColor.Z, target.Z, 0.5, "(Z)" + expectedColor.Z + " != " + target.Z); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IMunsell expectedColor) { var target = knownColor.To <Munsell>(); Assert.IsTrue(CloseEnough(expectedColor.H, target.H), expectedColor.ToString() + "!=" + target.ToString()); Assert.IsTrue(CloseEnough(expectedColor.V, target.V, 1.0), "(V)" + expectedColor.V + " != " + target.V); Assert.IsTrue(CloseEnough(expectedColor.C, target.C, 2.5), "(C)" + expectedColor.C + " != " + target.C); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILch expectedColor) { var target = knownColor.To <Lch>(); Assert.AreEqual(expectedColor.L, target.L, 0.5, "(L)" + expectedColor.L + " != " + target.L); Assert.AreEqual(expectedColor.C, target.C, 0.5, "(C)" + expectedColor.C + " != " + target.C); Assert.AreEqual(expectedColor.H, target.H, 1.8, "(H)" + expectedColor.H + " != " + target.H); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILab expectedColor) { var target = knownColor.To <Lab>(); Assert.IsTrue(CloseEnough(expectedColor.L, target.L), "(L)" + expectedColor.L + " != " + target.L); Assert.IsTrue(CloseEnough(expectedColor.A, target.A), "(A)" + expectedColor.A + " != " + target.A); Assert.IsTrue(CloseEnough(expectedColor.B, target.B), "(B)" + expectedColor.B + " != " + target.B); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILch expectedColor) { var target = knownColor.To <Lch>(); Assert.IsTrue(CloseEnough(expectedColor.L, target.L), "(L)" + expectedColor.L + " != " + target.L); Assert.IsTrue(CloseEnough(expectedColor.C, target.C), "(C)" + expectedColor.C + " != " + target.C); Assert.IsTrue(CloseEnough(expectedColor.H, target.H), "(H)" + expectedColor.H + " != " + target.H); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IHsl expectedColor) { var target = knownColor.To<Hsl>(); Assert.IsTrue(CloseEnough(expectedColor.H,target.H),"(H)" + expectedColor.H + " != " + target.H); Assert.IsTrue(CloseEnough(expectedColor.S,target.S),"(S)" + expectedColor.S + " != " + target.S); Assert.IsTrue(CloseEnough(expectedColor.L,target.L),"(L)" + expectedColor.L + " != " + target.L); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IRgb expectedColor) { var target = knownColor.To <Rgb>(); Assert.IsTrue(CloseEnough(expectedColor.R, target.R), "(R)" + expectedColor.R + " != " + target.R); Assert.IsTrue(CloseEnough(expectedColor.G, target.G), "(G)" + expectedColor.G + " != " + target.G); Assert.IsTrue(CloseEnough(expectedColor.B, target.B), "(B)" + expectedColor.B + " != " + target.B); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILch expectedColor) { var target = knownColor.To<Lch>(); Assert.IsTrue(CloseEnough(expectedColor.L,target.L),"(L)" + expectedColor.L + " != " + target.L); Assert.IsTrue(CloseEnough(expectedColor.C,target.C),"(C)" + expectedColor.C + " != " + target.C); Assert.IsTrue(CloseEnough(expectedColor.H,target.H),"(H)" + expectedColor.H + " != " + target.H); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IXyz expectedColor) { var target = knownColor.To <Xyz>(); Assert.IsTrue(CloseEnough(expectedColor.X, target.X), "(X)" + expectedColor.X + " != " + target.X); Assert.IsTrue(CloseEnough(expectedColor.Y, target.Y), "(Y)" + expectedColor.Y + " != " + target.Y); Assert.IsTrue(CloseEnough(expectedColor.Z, target.Z), "(Z)" + expectedColor.Z + " != " + target.Z); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IXyz expectedColor) { var target = knownColor.To<Xyz>(); Assert.IsTrue(CloseEnough(expectedColor.X,target.X),"(X)" + expectedColor.X + " != " + target.X); Assert.IsTrue(CloseEnough(expectedColor.Y,target.Y),"(Y)" + expectedColor.Y + " != " + target.Y); Assert.IsTrue(CloseEnough(expectedColor.Z,target.Z),"(Z)" + expectedColor.Z + " != " + target.Z); }
public static IColorSpaceWithProfile WithProfile(this IColorSpace colorSpace, Uri profile) { return(new ColorSpaceWithProfile { Color = colorSpace, Profile = profile }); }
public double Compare(IColorSpace colorA, IColorSpace colorB) { var a = colorA.To<Lab>(); var b = colorB.To<Lab>(); var differences = Distance(a.L, b.L) + Distance(a.A, b.A) + Distance(a.B, b.B); return Math.Sqrt(differences); }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, IHsl expectedColor) { var target = knownColor.To <Hsl>(); Assert.AreEqual(expectedColor.H, target.H, 1.8, "(H)" + expectedColor.H + " != " + target.H); Assert.AreEqual(expectedColor.S, target.S, 0.5, "(S)" + expectedColor.S + " != " + target.S); Assert.AreEqual(expectedColor.L, target.L, 0.5, "(L)" + expectedColor.L + " != " + target.L); }
/// <summary> /// Calculates the DE2000 delta-e value: http://en.wikipedia.org/wiki/Color_difference#CIEDE2000 /// </summary> public double Compare(IColorSpace colorA, IColorSpace colorB) { const double kl = 1.0; const double kc = 1.0; const double kh = 1.0; var lab1 = colorA.To<Lab>(); var lab2 = colorB.To<Lab>(); #region Get deltaLPrime, deltaCPrime, deltaHPrime var lBar = (lab1.L + lab2.L) / 2.0; var c1 = Math.Sqrt(lab1.A * lab1.A + lab1.B * lab1.B); var c2 = Math.Sqrt(lab2.A * lab2.A + lab2.B * lab2.B); var cBar = (c1 + c2) / 2.0; var cBarInPower7 = cBar * cBar * cBar; cBarInPower7 *= cBarInPower7 * cBar; var g = (1 - Math.Sqrt(cBarInPower7 / (cBarInPower7 + 6103515625))); // 25 ^ 7 var aPrime1 = lab1.A * (lab1.A / 2.0) * g; var aPrime2 = lab2.A * (lab2.A / 2.0) * g; var cPrime1 = Math.Sqrt(aPrime1 * aPrime1 + lab1.B * lab1.B); var cPrime2 = Math.Sqrt(aPrime2 * aPrime2 + lab2.B * lab2.B); var cBarPrime = (cPrime1 + cPrime2) / 2.0; var hPrime1 = Math.Atan2(lab1.B, aPrime1) % 360; var hPrime2 = Math.Atan2(lab2.B, aPrime2) % 360; var hBar = Math.Abs(hPrime1 - hPrime2); double deltaHPrime; if (hBar <= 180) { deltaHPrime = hPrime2 - hPrime1; } else if (hBar > 180 && hPrime2 <= hPrime1) { deltaHPrime = hPrime2 - hPrime1 + 360.0; } else { deltaHPrime = hPrime2 - hPrime1 - 360.0; } var deltaLPrime = lab2.L - lab1.L; var deltaCPrime = cPrime2 - cPrime1; deltaHPrime = 2 * Math.Sqrt(cPrime1 * cPrime2) * Math.Sin(deltaHPrime / 2.0); #endregion Get deltaLPrime, deltaCPrime, deltaHPrime var hBarPrime = hBar > 180 ? (hPrime1 + hPrime2 + 360) / 2.0 : (hPrime1 + hPrime2) / 2.0; var t = 1 - .17 * Math.Cos(hBarPrime - 30) + .24 * Math.Cos(2 * hBarPrime) + .32 * Math.Cos(3 * hBarPrime + 6) - .2 * Math.Cos(4 * hBarPrime - 63); double lBarMinus50Sqr = (lBar - 50) * (lBar - 50); var sl = 1 + (.015 * lBarMinus50Sqr) / Math.Sqrt(20 + lBarMinus50Sqr); var sc = 1 + .045 * cBarPrime; var sh = 1 + .015 * cBarPrime * t; double cBarPrimeInPower7 = cBarPrime * cBarPrime * cBarPrime; cBarPrimeInPower7 *= cBarPrimeInPower7 * cBarPrime; var rt = -2 * Math.Sqrt(cBarPrimeInPower7 / (cBarPrimeInPower7 + 6103515625)) // 25 ^ 7 * Math.Sin(60.0 * Math.Exp(-((hBarPrime - 275.0) / 25.0))); double deltaLPrimeDivklsl = deltaLPrime / (kl * sl); double deltaCPrimeDivkcsc = deltaCPrime / (kc * sc); double deltaHPrimeDivkhsh = deltaHPrime / (kh * sh); var deltaE = Math.Sqrt( deltaLPrimeDivklsl * deltaLPrimeDivklsl + deltaCPrimeDivkcsc * deltaCPrimeDivkcsc + deltaHPrimeDivkhsh * deltaHPrimeDivkhsh + rt * (deltaCPrime / (kc * kh)) * (deltaHPrime / (kh * sh))); return deltaE; }
private static void ReturnsExpectedValueForKnownInput(double expectedValue, Cie94Comparison.Application application, IColorSpace a, IColorSpace b) { var target = new Cie94Comparison(application); var actualValue = a.Compare(b, target); Assert.IsTrue(expectedValue.BasicallyEqualTo(actualValue), expectedValue + " != " + actualValue); }
/// <summary> /// Calculates the DE2000 delta-e value: http://en.wikipedia.org/wiki/Color_difference#CIEDE2000 /// Correct implementation provided courtesy of Jonathan Hofinger, jaytar42 /// </summary> public double Compare(IColorSpace c1, IColorSpace c2) { //Set weighting factors to 1 double k_L = 1.0d; double k_C = 1.0d; double k_H = 1.0d; //Change Color Space to L*a*b: Lab lab1 = c1.To<Lab>(); Lab lab2 = c2.To<Lab>(); //Calculate Cprime1, Cprime2, Cabbar double c_star_1_ab = Math.Sqrt(lab1.A * lab1.A + lab1.B * lab1.B); double c_star_2_ab = Math.Sqrt(lab2.A * lab2.A + lab2.B * lab2.B); double c_star_average_ab = (c_star_1_ab + c_star_2_ab) / 2; double c_star_average_ab_pot7 = c_star_average_ab * c_star_average_ab * c_star_average_ab; c_star_average_ab_pot7 *= c_star_average_ab_pot7 * c_star_average_ab; double G = 0.5d * (1 - Math.Sqrt(c_star_average_ab_pot7 / (c_star_average_ab_pot7 + 6103515625))); //25^7 double a1_prime = (1 + G) * lab1.A; double a2_prime = (1 + G) * lab2.A; double C_prime_1 = Math.Sqrt(a1_prime * a1_prime + lab1.B * lab1.B); double C_prime_2 = Math.Sqrt(a2_prime * a2_prime + lab2.B * lab2.B); //Angles in Degree. double h_prime_1 = ((Math.Atan2(lab1.B, a1_prime) * 180d / Math.PI) + 360) % 360d; double h_prime_2 = ((Math.Atan2(lab2.B, a2_prime) * 180d / Math.PI) + 360) % 360d; double delta_L_prime = lab2.L - lab1.L; double delta_C_prime = C_prime_2 - C_prime_1; double h_bar = Math.Abs(h_prime_1 - h_prime_2); double delta_h_prime; if (C_prime_1 * C_prime_2 == 0) delta_h_prime = 0; else { if (h_bar <= 180d) { delta_h_prime = h_prime_2 - h_prime_1; } else if (h_bar > 180d && h_prime_2 <= h_prime_1) { delta_h_prime = h_prime_2 - h_prime_1 + 360.0; } else { delta_h_prime = h_prime_2 - h_prime_1 - 360.0; } } double delta_H_prime = 2 * Math.Sqrt(C_prime_1 * C_prime_2) * Math.Sin(delta_h_prime * Math.PI / 360d); // Calculate CIEDE2000 double L_prime_average = (lab1.L + lab2.L) / 2d; double C_prime_average = (C_prime_1 + C_prime_2) / 2d; //Calculate h_prime_average double h_prime_average; if (C_prime_1 * C_prime_2 == 0) h_prime_average = 0; else { if (h_bar <= 180d) { h_prime_average = (h_prime_1 + h_prime_2) / 2; } else if (h_bar > 180d && (h_prime_1 + h_prime_2) < 360d) { h_prime_average = (h_prime_1 + h_prime_2 + 360d) / 2; } else { h_prime_average = (h_prime_1 + h_prime_2 - 360d) / 2; } } double L_prime_average_minus_50_square = (L_prime_average - 50); L_prime_average_minus_50_square *= L_prime_average_minus_50_square; double S_L = 1 + ((.015d * L_prime_average_minus_50_square) / Math.Sqrt(20 + L_prime_average_minus_50_square)); double S_C = 1 + .045d * C_prime_average; double T = 1 - .17 * Math.Cos(DegToRad(h_prime_average - 30)) + .24 * Math.Cos(DegToRad(h_prime_average * 2)) + .32 * Math.Cos(DegToRad(h_prime_average * 3 + 6)) - .2 * Math.Cos(DegToRad(h_prime_average * 4 - 63)); double S_H = 1 + .015 * T * C_prime_average; double h_prime_average_minus_275_div_25_square = (h_prime_average - 275) / (25); h_prime_average_minus_275_div_25_square *= h_prime_average_minus_275_div_25_square; double delta_theta = 30 * Math.Exp(-h_prime_average_minus_275_div_25_square); double C_prime_average_pot_7 = C_prime_average * C_prime_average * C_prime_average; C_prime_average_pot_7 *= C_prime_average_pot_7 * C_prime_average; double R_C = 2 * Math.Sqrt(C_prime_average_pot_7 / (C_prime_average_pot_7 + 6103515625)); double R_T = -Math.Sin(DegToRad(2 * delta_theta)) * R_C; double delta_L_prime_div_k_L_S_L = delta_L_prime / (S_L * k_L); double delta_C_prime_div_k_C_S_C = delta_C_prime / (S_C * k_C); double delta_H_prime_div_k_H_S_H = delta_H_prime / (S_H * k_H); double CIEDE2000 = Math.Sqrt( delta_L_prime_div_k_L_S_L * delta_L_prime_div_k_L_S_L + delta_C_prime_div_k_C_S_C * delta_C_prime_div_k_C_S_C + delta_H_prime_div_k_H_S_H * delta_H_prime_div_k_H_S_H + R_T * delta_C_prime_div_k_C_S_C * delta_H_prime_div_k_H_S_H ); return CIEDE2000; }
public static Color4 ColorSpaceToColor4(IColorSpace col) { var rgb = col.ToRgb(); return new Color4((byte)rgb.R, (byte)rgb.G, (byte)rgb.B, 255); }
private void ReturnsExpectedValueForKnownInput(double expectedValue, double ratio, IColorSpace a, IColorSpace b) { var target = new CmcComparison(ratio); var actualValue = a.Compare(b, target); Assert.IsTrue(expectedValue.BasicallyEqualTo(actualValue),expectedValue + " != " + actualValue); }
internal ColorChannelDefinition(IColorSpace colorSpace, string channelName, string shortChannelName) : this(colorSpace, channelName, shortChannelName, (v) => v) { }
protected static void ExpectedValuesForKnownColor(IColorSpace knownColor, ILuv expectedColor) { var target = knownColor.To<Luv>(); Assert.IsTrue(CloseEnough(expectedColor.L,target.L),"(L)" + expectedColor.L + " != " + target.L); Assert.IsTrue(CloseEnough(expectedColor.U,target.U),"(U)" + expectedColor.U + " != " + target.U); Assert.IsTrue(CloseEnough(expectedColor.V,target.V),"(V)" + expectedColor.V + " != " + target.V); }