/// <summary>
        /// Adapts a <see cref="LinearRgb"/> color from the source working space to working space set in <see cref="TargetRgbWorkingSpace"/>.
        /// </summary>
        /// <param name="color">The color to adapt</param>
        /// <returns>The adapted color</returns>
        public LinearRgb Adapt(LinearRgb color)
        {
            if (!this.IsChromaticAdaptationPerformed)
            {
                throw new InvalidOperationException("Cannot perform chromatic adaptation, provide a chromatic adaptation method and white point.");
            }

            if (color.WorkingSpace.Equals(this.TargetRgbWorkingSpace))
            {
                return(color);
            }

            // Conversion to XYZ
            LinearRgbToCieXyzConverter converterToXYZ = this.GetLinearRgbToCieXyzConverter(color.WorkingSpace);
            CieXyz unadapted = converterToXYZ.Convert(color);

            // Adaptation
            CieXyz adapted = this.ChromaticAdaptation.Transform(unadapted, color.WorkingSpace.WhitePoint, this.TargetRgbWorkingSpace.WhitePoint);

            // Conversion back to RGB
            CieXyzToLinearRgbConverter converterToRGB = this.GetCieXyxToLinearRgbConverter(this.TargetRgbWorkingSpace);

            return(converterToRGB.Convert(adapted));
        }
        public void Convert_XYZ_to_xyY(float xyzX, float xyzY, float xyzZ, float x, float y, float yl)
        {
            var input    = new CieXyz(xyzX, xyzY, xyzZ);
            var expected = new CieXyy(x, y, yl);

            Span <CieXyz> inputSpan = new CieXyz[5];

            inputSpan.Fill(input);

            Span <CieXyy> actualSpan = new CieXyy[5];

            // Act
            var actual = Converter.ToCieXyy(input);

            Converter.Convert(inputSpan, actualSpan);

            // Assert
            Assert.Equal(expected, actual, ColorSpaceComparer);

            for (int i = 0; i < actualSpan.Length; i++)
            {
                Assert.Equal(expected, actualSpan[i], ColorSpaceComparer);
            }
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CieXyzToCieLuvConverter"/> class.
 /// </summary>
 /// <param name="luvWhitePoint">The target reference luv white point</param>
 public CieXyzToCieLuvConverter(CieXyz luvWhitePoint) => this.LuvWhitePoint = luvWhitePoint;
        public static LabColor FromRgbColor(Color color)
        {
            int red = color.R;
            int green = color.G;
            int blue = color.B;

            // normalize red, green, blue values
            double rLinear = red/255.0;
            double gLinear = green/255.0;
            double bLinear = blue/255.0;

            // convert to a sRGB form
            double r = (rLinear > 0.04045)
                           ? Math.Pow((rLinear + 0.055)/(
                                                            1 + 0.055), 2.2)
                           : (rLinear/12.92);
            double g = (gLinear > 0.04045)
                           ? Math.Pow((gLinear + 0.055)/(
                                                            1 + 0.055), 2.2)
                           : (gLinear/12.92);
            double b = (bLinear > 0.04045)
                           ? Math.Pow((bLinear + 0.055)/(
                                                            1 + 0.055), 2.2)
                           : (bLinear/12.92);

            var xyz = new CieXyz(
                (r*0.4124 + g*0.3576 + b*0.1805),
                (r*0.2126 + g*0.7152 + b*0.0722),
                (r*0.0193 + g*0.1192 + b*0.9505)
                );
            double x = xyz.X;
            double y = xyz.Y;
            double z = xyz.Z;

            return new LabColor
                       {
                           L = 116.0*Fxyz(y/CieXyz.D65.Y) - 16,
                           a = 500.0*(Fxyz(x/CieXyz.D65.X) - Fxyz(y/CieXyz.D65.Y)),
                           b = 200.0*(Fxyz(y/CieXyz.D65.Y) - Fxyz(z/CieXyz.D65.Z))
                       };
        }
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Rec2020WorkingSpace" /> class.
 /// </summary>
 /// <param name="referenceWhite">The reference white point.</param>
 /// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
 public Rec2020WorkingSpace(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates)
     : base(referenceWhite, chromaticityCoordinates)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="GammaWorkingSpace" /> class.
 /// </summary>
 /// <param name="gamma">The gamma value.</param>
 /// <param name="referenceWhite">The reference white point.</param>
 /// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
 public GammaWorkingSpace(float gamma, CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates)
     : base(referenceWhite, chromaticityCoordinates) => this.Gamma = gamma;
Beispiel #7
0
        public void SingleAndBulkTransformYieldIdenticalResults(CieXyz sourceWhitePoint, CieXyz destinationWhitePoint)
        {
            var    adaptation = new VonKriesChromaticAdaptation();
            var    input      = new CieXyz(1, 0, 1);
            CieXyz expected   = adaptation.Transform(input, sourceWhitePoint, destinationWhitePoint);

            Span <CieXyz> inputSpan = new CieXyz[5];

            inputSpan.Fill(input);

            Span <CieXyz> actualSpan = new CieXyz[5];

            adaptation.Transform(inputSpan, actualSpan, sourceWhitePoint, destinationWhitePoint);

            for (int i = 0; i < inputSpan.Length; i++)
            {
                Assert.Equal(expected, actualSpan[i], ColorSpaceComparer);
            }
        }
        /// <summary>
        /// Converts a <see cref="CieXyz"/> into a <see cref="Hsl"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="Hsl"/></returns>
        public Hsl ToHsl(CieXyz color)
        {
            var rgb = this.ToRgb(color);

            return(HslAndRgbConverter.Convert(rgb));
        }
        /// <summary>
        /// Converts a <see cref="Hsl"/> into a <see cref="CieLch"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="CieLch"/></returns>
        public CieLch ToCieLch(Hsl color)
        {
            CieXyz xyzColor = this.ToCieXyz(color);

            return(this.ToCieLch(xyzColor));
        }
Beispiel #10
0
        /// <summary>
        /// Converts a <see cref="CieXyy"/> into a <see cref="CieLab"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="CieLab"/></returns>
        public CieLab ToCieLab(CieXyy color)
        {
            CieXyz xyzColor = this.ToCieXyz(color);

            return(this.ToCieLab(xyzColor));
        }
 /// <summary>
 /// Converts a <see cref="CieXyz"/> into a <see cref="CieXyy"/>
 /// </summary>
 /// <param name="color">The color to convert.</param>
 /// <returns>The <see cref="CieXyy"/></returns>
 public CieXyy ToCieXyy(CieXyz color)
 {
     return(CieXyzAndCieXyyConverter.Convert(color));
 }
Beispiel #12
0
        /// <summary>
        /// Converts a <see cref="CieXyz"/> into a <see cref="CieXyy"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="CieXyy"/></returns>
        public CieXyy ToCieXyy(CieXyz color)
        {
            Guard.NotNull(color, nameof(color));

            return(CieXyzAndCieXyyConverter.Convert(color));
        }
Beispiel #13
0
        /// <summary>
        /// Converts a <see cref="CieXyz"/> into a <see cref="Cmyk"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="Cmyk"/></returns>
        public Cmyk ToCmyk(CieXyz color)
        {
            var rgb = this.ToRgb(color);

            return(CmykAndRgbConverter.Convert(rgb));
        }
 /// <summary>
 /// Converts a <see cref="CieXyz"/> into a <see cref="Lms"/>
 /// </summary>
 /// <param name="color">The color to convert.</param>
 /// <returns>The <see cref="Lms"/></returns>
 public Lms ToLms(CieXyz color)
 {
     return(this.cachedCieXyzAndLmsConverter.Convert(color));
 }
        /// <summary>
        /// Converts a <see cref="LinearRgb"/> into a <see cref="CieLch"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="CieLch"/></returns>
        public CieLch ToCieLch(LinearRgb color)
        {
            CieXyz xyzColor = this.ToCieXyz(color);

            return(this.ToCieLch(xyzColor));
        }
        /// <summary>
        /// Converts a <see cref="CieXyz"/> into a <see cref="CieLch"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="CieLch"/></returns>
        public CieLch ToCieLch(CieXyz color)
        {
            CieLab labColor = this.ToCieLab(color);

            return(this.ToCieLch(labColor));
        }
Beispiel #17
0
 public bool Equals(CieXyz x, CieXyz y)
 {
     return(this.Equals(x.X, y.X) && this.Equals(x.Y, y.Y) && this.Equals(x.Z, y.Z));
 }
Beispiel #18
0
        /// <summary>
        /// Converts a <see cref="CieXyz"/> into a <see cref="YCbCr"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="YCbCr"/></returns>
        public YCbCr ToYCbCr(CieXyz color)
        {
            var rgb = this.ToRgb(color);

            return(YCbCrAndRgbConverter.Convert(rgb));
        }
Beispiel #19
0
 public int GetHashCode(CieXyz obj)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CieXyzToHunterLabConverter"/> class.
 /// </summary>
 /// <param name="labWhitePoint">The hunter Lab white point.</param>
 public CieXyzToHunterLabConverter(CieXyz labWhitePoint) => this.HunterLabWhitePoint = labWhitePoint;
        /// <summary>
        /// Converts a <see cref="HunterLab"/> into a <see cref="CieLchuv"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="CieLchuv"/></returns>
        public CieLchuv ToCieLchuv(HunterLab color)
        {
            CieXyz xyzColor = this.ToCieXyz(color);

            return(this.ToCieLchuv(xyzColor));
        }
        /// <summary>
        /// Converts a <see cref="Cmyk"/> into a <see cref="CieLchuv"/>
        /// </summary>
        /// <param name="color">The color to convert.</param>
        /// <returns>The <see cref="CieLchuv"/></returns>
        public CieLchuv ToCieLchuv(Cmyk color)
        {
            CieXyz xyzColor = this.ToCieXyz(color);

            return(this.ToCieLchuv(xyzColor));
        }
Beispiel #23
0
 public CieXyzTriple(BinaryReader rdr)
 {
     cieXyzRed   = new CieXyz(rdr);
     cieXyzGreen = new CieXyz(rdr);
     cieXyzBlue  = new CieXyz(rdr);
 }
Beispiel #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RgbWorkingSpace"/> struct.
 /// </summary>
 /// <param name="referenceWhite">The reference white point.</param>
 /// <param name="companding">The function pair for converting to <see cref="CieXyz"/> and back.</param>
 /// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
 public RgbWorkingSpace(CieXyz referenceWhite, ICompanding companding, RgbPrimariesChromaticityCoordinates chromaticityCoordinates)
 {
     this.WhitePoint = referenceWhite;
     this.Companding = companding;
     this.ChromaticityCoordinates = chromaticityCoordinates;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RgbWorkingSpaceBase"/> class.
 /// </summary>
 /// <param name="referenceWhite">The reference white point.</param>
 /// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
 protected RgbWorkingSpaceBase(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates)
 {
     this.WhitePoint = referenceWhite;
     this.ChromaticityCoordinates = chromaticityCoordinates;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CieXyzToCieLabConverter"/> class.
 /// </summary>
 /// <param name="labWhitePoint">The target reference lab white point</param>
 public CieXyzToCieLabConverter(CieXyz labWhitePoint) => this.LabWhitePoint = labWhitePoint;
        public Color ToRgbColor()
        {
            double delta = 6.0 / 29.0;

            double fy = (L + 16) / 116.0;
            double fx = fy + (a / 500.0);
            double fz = fy - (b / 200.0);

            var xyz = new CieXyz(
                (fx > delta) ? CieXyz.D65.X * (fx * fx * fx) : (fx - 16.0 / 116.0) * 3 * (
                    delta * delta) * CieXyz.D65.X,
                (fy > delta) ? CieXyz.D65.Y * (fy * fy * fy) : (fy - 16.0 / 116.0) * 3 * (
                    delta * delta) * CieXyz.D65.Y,
                (fz > delta) ? CieXyz.D65.Z * (fz * fz * fz) : (fz - 16.0 / 116.0) * 3 * (
                    delta * delta) * CieXyz.D65.Z
                );
            double x = xyz.X;
            double y = xyz.Y;
            double z = xyz.Z;

            double[] Clinear = new double[3];
            Clinear[0] = x*3.2410 - y*1.5374 - z*0.4986; // red
            Clinear[1] = -x*0.9692 + y*1.8760 - z*0.0416; // green
            Clinear[2] = x*0.0556 - y*0.2040 + z*1.0570; // blue

            for(int i=0; i<3; i++)
            {
                Clinear[i] = (Clinear[i]<=0.0031308)? 12.92*Clinear[i] : (
                    1+0.055)* Math.Pow(Clinear[i], (1.0/2.4)) - 0.055;
            }

            return Color.FromArgb(
                Convert.ToInt32( Double.Parse(String.Format("{0:0.00}",
                    Clinear[0]*255.0)) ),
                Convert.ToInt32( Double.Parse(String.Format("{0:0.00}",
                    Clinear[1]*255.0)) ),
                Convert.ToInt32( Double.Parse(String.Format("{0:0.00}",
                    Clinear[2]*255.0)) )
                );
        }