/// <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) { Guard.NotNull(color, nameof(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)); }
/// <summary> /// Gets the correct converter for the given rgb working space. /// </summary> /// <param name="workingSpace">The source working space</param> /// <returns>The <see cref="LinearRgbToCieXyzConverter"/></returns> private LinearRgbToCieXyzConverter GetLinearRgbToCieXyzConverter(IRgbWorkingSpace workingSpace) { if (this.linearRgbToCieXyzConverter != null && this.linearRgbToCieXyzConverter.SourceWorkingSpace.Equals(workingSpace)) { return(this.linearRgbToCieXyzConverter); } return(this.linearRgbToCieXyzConverter = new LinearRgbToCieXyzConverter(workingSpace)); }
/// <summary> /// Converts a <see cref="LinearRgb"/> into a <see cref="CieXyz"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="CieXyz"/></returns> public CieXyz ToCieXyz(LinearRgb color) { Guard.NotNull(color, nameof(color)); // Conversion LinearRgbToCieXyzConverter converter = this.GetLinearRgbToCieXyzConverter(color.WorkingSpace); CieXyz unadapted = converter.Convert(color); // Adaptation return(color.WorkingSpace.WhitePoint.Equals(this.WhitePoint) || !this.IsChromaticAdaptationPerformed ? unadapted : this.Adapt(unadapted, color.WorkingSpace.WhitePoint)); }