using System;
namespace Colourful.Implementation.Conversion
{
///
/// Converts from to .
///
public sealed class XYZToHunterLabConverter : XYZAndHunterLabConverterBase, IColorConversion
{
///
/// Construct with
///
public XYZToHunterLabConverter()
: this(HunterLabColor.DefaultWhitePoint)
{
}
///
/// Construct with arbitrary white point
///
public XYZToHunterLabConverter(XYZColor labWhitePoint)
{
HunterLabWhitePoint = labWhitePoint;
}
///
/// Target reference white. When not set, is used.
///
public XYZColor HunterLabWhitePoint { get; }
///
/// Converts from to .
///
public HunterLabColor Convert(in XYZColor input)
{
// conversion algorithm described here: http://en.wikipedia.org/wiki/Lab_color_space#Hunter_Lab
double X = input.X, Y = input.Y, Z = input.Z;
double Xn = HunterLabWhitePoint.X, Yn = HunterLabWhitePoint.Y, Zn = HunterLabWhitePoint.Z;
var Ka = ComputeKa(HunterLabWhitePoint);
var Kb = ComputeKb(HunterLabWhitePoint);
var L = 100 * Math.Sqrt(Y / Yn);
var a = Ka * ((X / Xn - Y / Yn) / Math.Sqrt(Y / Yn));
var b = Kb * ((Y / Yn - Z / Zn) / Math.Sqrt(Y / Yn));
if (double.IsNaN(a))
a = 0;
if (double.IsNaN(b))
b = 0;
var output = new HunterLabColor(L, a, b, HunterLabWhitePoint);
return output;
}
}
}