Class ColorUtils
- Namespace
- NixUniversalSDK
- Assembly
- NixUniversalSDK.dll
Color math utility functions
public static class ColorUtils
- Inheritance
-
ColorUtils
- Inherited Members
Methods
AdaptXyz(double[], double[], double[])
Adapts colour in XYZ from one reference white to another using Bradford adaptation method. NOTE: refIn, refOut, xyzIn must all be on the same scale
public static double[] AdaptXyz(double[] xyzIn, double[] refIn, double[] refOut)
Parameters
xyzIn
double[]Source color, referenced to refIn (element order X, Y, Z)
refIn
double[]Source reference white (element order X, Y, Z)
refOut
double[]Destination reference white (element order X, Y, Z)
Returns
- double[]
Adapted color, referenced to refOut (element order X, Y, Z)
ClipXyz(double[], double[])
Clips CIEXYZ values in between 0 and the value of the reference white to ensure X, Y, Z are in the proper range.
public static double[] ClipXyz(double[] xyz, double[] xyzRef)
Parameters
xyz
double[]CIEXYZ value (element order X, Y, Z)
xyzRef
double[]CIEXYZ value for reference white (X, Y, Z)
Returns
- double[]
Clipped CIEXYZ value (element order X, Y, Z)
CmykToXyz(double[], double[], bool)
Calculates XYZ value from CMYK input using built-in lookup tables
public static double[] CmykToXyz(double[] cmykIn, double[] refOut, bool bpc)
Parameters
cmykIn
double[]CMYK colour input, unity scale (element order C, M, Y, K)
refOut
double[]Reference white value for XYZ output, unity scale (element order X, Y, Z)
bpc
boolSet to true to perform black point compensation
Returns
- double[]
XYZ value output, unity scale (element order X, Y, Z)
CmykToXyz(double[], double[], bool, double[][], int[][], int[][], int[][])
Calculates XYZ value from CMYK input using lookup tables provided as input arguments
public static double[] CmykToXyz(double[] cmykIn, double[] refOut, bool bpc, double[][] m, int[][] input, int[][] clut, int[][] output)
Parameters
cmykIn
double[]CMYK colour input, unity scale (element order C, M, Y, K)
refOut
double[]Reference white value for XYZ output, unity scale (element order X, Y, Z)
bpc
boolSet to true to perform black point compensation
m
double[][]3x3 Extracted from ICC profile: transformation matrix to perform on LAB value (NOT used in current function)
input
int[][]256x4 Extracted from ICC profile: InputCurves
clut
int[][]6561x3 Extracted from ICC profile: CMYK to LAB LUT (flattened 9x9x9x9 array with 3 channels, one each for L, A, B)
output
int[][]Extracted from ICC profile: OutputCurves (NOT used in current function, default profile has 1:1 output)
Returns
- double[]
XYZ value output, unity scale (element order X, Y, Z)
DegToRad(double)
Converts angle in degrees to radians (used in dE2000 function)
public static double DegToRad(double deg)
Parameters
deg
doubleAngle in degrees
Returns
- double
Angle in radians
DeltaE00(double[], double[], double, double, double)
Takes two CIELAB values and calculates color difference (dE) using the CIE2000 formula. NOTE: both CIELAB values must share the same reference white. If they do not, convert to XYZ, adapt, convert back to CIELAB before calculating dE.
public static double DeltaE00(double[] lab1, double[] lab2, double kL = 1, double kC = 1, double kH = 1)
Parameters
lab1
double[]First CIELAB value (element order L, a, b)
lab2
double[]Second CIELAB value (element order L, a, b)
kL
doubleModel parameter (optional, default 1)
kC
doubleModel parameter (optional, default 1)
kH
doubleModel parameter (optional, default 1)
Returns
- double
Delta E (2000) value
DeltaE76(double[], double[])
Takes two CIELAB values and calculates color difference (dE) using CIE1976 formula. NOTE: both CIELAB values must share the same reference white. If they do not: convert to XYZ, adapt, convert back to CIELAB before calculating dE.
public static double DeltaE76(double[] lab1, double[] lab2)
Parameters
lab1
double[]First CIELAB value (element order L, a, b)
lab2
double[]Second CIELAB value (element order L, a, b)
Returns
- double
Delta E (1976) value
DeltaE94(double[], double[], double, double, double)
Takes two CIELAB values and calculates color difference (dE) using the CIE1994 formula. Requires weighting factors kL, k1, and k2 which are dependent on application. Use DeltaE94G(double[], double[]) for graphic design applications andDeltaE94T(double[], double[]) for textile applications. Both CIELAB values must share the same reference white.If they do not, convert to XYZ, adapt, convert back to CIELAB before calculating dE.
public static double DeltaE94(double[] lab1, double[] lab2, double kL, double k1, double k2)
Parameters
lab1
double[]First CIELAB value (element order L, a, b)
lab2
double[]Second CIELAB value (element order L, a, b)
kL
doubleWeighting factor (application dependent)
k1
doubleWeighting factor (application dependent)
k2
doubleWeighting factor (application dependent)
Returns
- double
Delta E (1994) value
DeltaE94G(double[], double[])
Takes two CIELAB values and calculates color difference (dE) using the CIE1994 formula for the graphic design application (kL = 1, k1 = 0.045, k2 = 0.015). See also DeltaE94(double[], double[], double, double, double)
public static double DeltaE94G(double[] lab1, double[] lab2)
Parameters
lab1
double[]First CIELAB value (element order L, a, b)
lab2
double[]Second CIELAB value (element order L, a, b)
Returns
- double
Delta E (1994) value
DeltaE94T(double[], double[])
Takes two CIELAB values and calculates color difference (dE) using the CIE1994 formula for the textiles design application (kL = 2, k1 = 0.048, k2 = 0.014). See also DeltaE94(double[], double[], double, double, double)
public static double DeltaE94T(double[] lab1, double[] lab2)
Parameters
lab1
double[]First CIELAB value (element order L, a, b)
lab2
double[]Second CIELAB value (element order L, a, b)
Returns
- double
Delta E (1994) value
DeltaECmc(double[], double[], double, double)
Takes two CIELAB values and calculates color difference (dE) using the CMC(l:c) formula. Depends on model parameters l and c; common values used are CMC(2:1) for acceptability and CMC(1:1) for perceptibility.
public static double DeltaECmc(double[] lab1, double[] lab2, double l, double c)
Parameters
lab1
double[]First CIELAB value (element order L, a, b)
lab2
double[]First CIELAB value (element order L, a, b)
l
doubleModel parameter l
c
doubleModel parameter c
Returns
- double
Delta E (CMC) value
LabToLch(double[])
Takes CIELAB value and converts to polar coordinates (LCH)
public static double[] LabToLch(double[] labIn)
Parameters
labIn
double[]CIELAB value (element order L, a, b)
Returns
- double[]
LCHab value (element order L, c, h). Hue angle h is in degrees
LabToXyz(double[], Illuminant, Observer)
Takes CIELAB value and its reference white to calculate CIEXYZ value
public static double[] LabToXyz(double[] lab, Illuminant illuminant, Observer observer)
Parameters
lab
double[]illuminant
IlluminantIlluminant for reference white point
observer
ObserverObserver for reference white point
Returns
- double[]
CIEXYZ value (element order X, Y, Z)
LabToXyz(double[], ReferenceWhite)
Takes CIELAB value and its reference white to calculate CIEXYZ value
public static double[] LabToXyz(double[] lab, ReferenceWhite reference)
Parameters
lab
double[]CIELAB value (element order L, a, b)
reference
ReferenceWhiteReference white point for input CIEXYZ value
Returns
- double[]
CIEXYZ value (element order X, Y, Z)
LabToXyz(double[], double[])
Takes CIELAB value and its reference white to calculate CIEXYZ value
public static double[] LabToXyz(double[] lab, double[] xyzRef)
Parameters
lab
double[]CIELAB value (element order L, a, b)
xyzRef
double[]Reference white (element order X, Y, Z)
Returns
- double[]
CIEXYZ value (element order X, Y, Z)
LchToLab(double[])
Takes LCHab value and converts to CIELAB
public static double[] LchToLab(double[] lchIn)
Parameters
lchIn
double[]LCHab value (element order L, c, h). Hue angle h is in degrees
Returns
- double[]
CIELAB value (element order L, a, b)
LchToLuv(double[])
Takes LCHuv value and converts to CIELUV
public static double[] LchToLuv(double[] lchIn)
Parameters
lchIn
double[]LCHuv value (element order L, c, h). Hue angle h is in degrees
Returns
- double[]
CIELUV value (element order L, u, v)
LuvToLch(double[])
Takes CIELUV value and converts to polar coordinates (LCH)
public static double[] LuvToLch(double[] LuvIn)
Parameters
LuvIn
double[]CIELUV value (element order L, u, v)
Returns
- double[]
LCHuv value (element order L, c, h). Hue angle h is in degrees
LuvToXyz(double[], double[])
Takes CIELUV value and its reference white to calculate CIEXYZ value
public static double[] LuvToXyz(double[] luvIn, double[] xyzRef)
Parameters
luvIn
double[]CIELUV value (element order L, u, v)
xyzRef
double[]CIEXYZ reference white (element order X, Y, Z)
Returns
- double[]
CIEXYZ value (element order X, Y, Z)
RadToDeg(double)
Converts angle in radians to degrees (used in dE2000 function)
public static double RadToDeg(double rads)
Parameters
rads
doubleAngle in radians
Returns
- double
Angle in degrees
RgbToXyz(double[], double[], double[], double[], double[], double[])
Calculates XYZ from linear RGB given chromaticity coordinates of RGB working space primaries and reference whites of RGB and XYZ value.
public static double[] RgbToXyz(double[] rgbIn, double[] rC, double[] gC, double[] bC, double[] wC, double[] refOut)
Parameters
rgbIn
double[]Input linear RGB value, unity scale (element order R, G, B)
rC
double[]RGB space red primary chromaticity coordinates (element order x, y)
gC
double[]RGB space green primary chromaticity coordinates (element order x, y)
bC
double[]RGB space blue primary chromaticity coordinates (element order x, y)
wC
double[]RGB space reference white/grey chromaticity coordinates (element order x, y)
refOut
double[]Selected reference white of output XYZ color, unity scale (element order X, Y, Z)
Returns
- double[]
XYZ value, unity scale, with reference white refOut (element order X, Y, Z)
RgbTripletToHexString(byte[])
Takes a RGB triplet and formats it as a HEX string (e.g. - "#RRGGBB")
public static string RgbTripletToHexString(byte[] rgb)
Parameters
rgb
byte[]
Returns
Exceptions
- ArgumentException
Thrown if array does not have three entries
SRgbToXyz(byte[], double[])
Calculates XYZ from sRGB value
public static double[] SRgbToXyz(byte[] sRgb, double[] refOut)
Parameters
sRgb
byte[]Non-linear sRGB value, 0 - 255 scale (element order R, G, B)
refOut
double[]Reference white of colour output, unity scale (element order X, Y, Z)
Returns
- double[]
XYZ value, unity scale, with reference white refOut (element order X, Y, Z)
Spectral10ToDensity(int[], float[], DensityStatus)
Takes reflectance data on 10 nm intervals and calculates CMYK densities according to ISO 5-3:2009. Data must be provided on 10 nm intervals corresponding to the nearest 10 nanometer. Density values are calculated across the range 340 - 770 nm. If the provided data spans a shorter range (e.g. - 400 - 700 nm), the values at the boundaries are repeated.
public static double[] Spectral10ToDensity(int[] lambda, float[] reflectance, DensityStatus status)
Parameters
lambda
int[]Wavelength values of provided reflectance data.
reflectance
float[]Reflectance data. Must contain the same number of elements as lambda
status
DensityStatusSelected density status
Returns
- double[]
CMYK densities (element order C, M, Y, K)
Spectral10ToXyz(int[], float[], Illuminant, Observer)
Takes reflectance data on 10 nm intervals and calculates a CIEXYZ value according to ASTM E308. Data must be provided on 10 nm intervals corresponding to the nearest 10 nanometer. CIEXYZ values are calculated across the range 360 - 780 nm.If the provided data spans a shorter range (e.g. - 400 - 700 nm), the values at the boundaries are repeated.
public static double[] Spectral10ToXyz(int[] lambda, float[] reflectance, Illuminant illuminant, Observer observer)
Parameters
lambda
int[]Wavelength values of provided reflectance data.
reflectance
float[]Reflectance data. Must contain the same number of elements as lambda
illuminant
IlluminantReference illuminant
observer
ObserverReference observer
Returns
- double[]
CIEXYZ value reported on 0 - 1.0 scale (element order X, Y, Z)
Spectral10ToXyz(int[], float[], ReferenceWhite)
Takes reflectance data on 10 nm intervals and calculates a CIEXYZ value according to ASTM E308. Data must be provided on 10 nm intervals corresponding to the nearest 10 nanometer. CIEXYZ values are calculated across the range 360 - 780 nm. If the provided data spans a shorter range (e.g. - 400 - 700 nm), the values at the boundaries are repeated.
public static double[] Spectral10ToXyz(int[] lambda, float[] reflectance, ReferenceWhite reference)
Parameters
lambda
int[]Wavelength values of provided reflectance data.
reflectance
float[]Reflectance data. Must contain the same number of elements as lambda
reference
ReferenceWhiteSelected reference white
Returns
- double[]
CIEXYZ value reported on 0 - 1.0 scale (element order X, Y, Z)
XyzChromaticity(double[])
Calculates chromaticity coordinates x,y from a given xyz value. WARNING: Be aware that for black xyz(0, 0, 0), the result is undefined. For this case, use chromaticity of reference white instead.
public static double[] XyzChromaticity(double[] xyz)
Parameters
xyz
double[]Source color (element order X, Y, Z)
Returns
- double[]
Chromaticity of input color, unity scale (element order x, y)
XyzToCmyk(double[], double[], bool)
Calculates CMYK value from XYZ input using built in lookup tables
public static double[] XyzToCmyk(double[] xyzIn, double[] xyzRef, bool bpc)
Parameters
xyzIn
double[]XYZ colour, unity scale (element order X, Y, Z)
xyzRef
double[]Reference white value for xyzIn, unity scale (element order X, Y, Z)
bpc
boolBlack point compensation enabled / disabled
Returns
- double[]
CMYK value on unity scale (element order C, M, Y, K)
XyzToCmyk(double[], double[], bool, double[][], int[][], int[][], int[][])
Calculates CMYK value from XYZ using lookup tables provided as inputCurves
arguments
public static double[] XyzToCmyk(double[] xyzIn, double[] xyzRef, bool bpc, double[][] m, int[][] inputCurves, int[][] clut, int[][] outputCurves)
Parameters
xyzIn
double[]XYZ colour, unity scale (element order X, Y, Z)
xyzRef
double[]Reference white value for xyzIn, unity scale (element order X, Y, Z)
bpc
boolBlack point compensation enabled / disabled
m
double[][]3x3 Extracted from ICC profile: transformation matrix to perform on LAB value (code only tested with identity matrix)
inputCurves
int[][]256x3 Extracted from ICC profile: InputCurves
clut
int[][]35937x4: Extracted from ICC profile: Lab to CMYK LUT (flattened 33x33x33 array with 4 channels, one each for C,M,Y,K)
outputCurves
int[][]256x4: Extracted from ICC profile: OutputCurves
Returns
- double[]
CMYK value on unity scale (element order C, M, Y, K)
XyzToLab(double[], Illuminant, Observer)
Takes CIEXYZ value and its reference white to calculate CIELAB value. Value xyz
and xyzRef
must be either unity (Y on 0 - 1) input.
public static double[] XyzToLab(double[] xyz, Illuminant illuminant, Observer observer)
Parameters
xyz
double[]illuminant
IlluminantIlluminant for reference white point
observer
ObserverObserver for reference white point
Returns
- double[]
CIELAB value (element order L, a, b)
XyzToLab(double[], ReferenceWhite)
Takes CIEXYZ value and its reference white to calculate CIELAB value. Value xyz
and xyzRef
must be either unity (Y on 0 - 1) input.
public static double[] XyzToLab(double[] xyz, ReferenceWhite reference)
Parameters
xyz
double[]CIEXYZ value (element order X, Y, Z)
reference
ReferenceWhiteReference white point for input CIEXYZ value
Returns
- double[]
CIELAB value (element order L, a, b)
XyzToLab(double[], double[])
Takes CIEXYZ value and its reference white to calculate CIELAB value. Values xyz
and xyzRef
can be either unity (0 - 1) or scaled (0 - 100) input, but both vectors must be the same scale. (i.e., if xyz
is unity scale, xyzRef
must also be unity scale)
public static double[] XyzToLab(double[] xyz, double[] xyzRef)
Parameters
xyz
double[]CIEXYZ value (element order X, Y, Z)
xyzRef
double[]CIEXYZ value for reference white (X, Y, Z)
Returns
- double[]
CIELAB value (element order L, a, b)
XyzToLuv(double[], double[])
Takes CIEXYZ value and its reference white to calculate CIELUV value.
public static double[] XyzToLuv(double[] xyzIn, double[] xyzRef)
Parameters
xyzIn
double[]CIEXYZ value (element order X, Y, Z)
xyzRef
double[]CIEXYZ reference white (element order X, Y, Z)
Returns
- double[]
CIELUV value (element order L, u, v)
XyzToRgb(double[], double[], double[], double[], double[], double[])
Calculates linear RGB from XYZ given chromaticity coordinates of RGB working space primaries and reference whites of RGB and XYZ value
public static double[] XyzToRgb(double[] xyzIn, double[] refIn, double[] rC, double[] gC, double[] bC, double[] wC)
Parameters
xyzIn
double[]Input colour XYZ values, unity scale (element order X, Y, Z)
refIn
double[]Reference white of input colour xyzIn, unity scale (element order X, Y, Z)
rC
double[]RGB space red primary chromaticity coordinates (element order x, y)
gC
double[]RGB space green primary chromaticity coordinates (element order x, y)
bC
double[]RGB space blue primary chromaticity coordinates (element order x, y)
wC
double[]RGB space reference white/grey chromaticity coordinates (element order x, y)
Returns
- double[]
XyzToSRgb(double[], double[])
Calculates companded sRGB value from XYZ given reference white of XYZ value.
public static byte[] XyzToSRgb(double[] xyzIn, double[] refIn)
Parameters
xyzIn
double[]Input colour XYZ values, unity scale (element order X, Y, Z)
refIn
double[]Reference white of input colour xyzIn, unity scale (element order X, Y, Z)
Returns
- byte[]
Non-linear sRGB value, 0 - 255 scale (element order R, G, B)