public final class ColorUtils extends Object
Color
.Modifier and Type | Method and Description |
---|---|
static int |
blendARGB(int color1,
int color2,
float ratio)
Blend between two ARGB colors using the given ratio.
|
static void |
blendHSL(float[] hsl1,
float[] hsl2,
float ratio,
float[] outResult)
Blend between
hsl1 and hsl2 using the given ratio. |
static void |
blendLAB(double[] lab1,
double[] lab2,
double ratio,
double[] outResult)
Blend between two CIE-LAB colors using the given ratio.
|
static double |
calculateContrast(int foreground,
int background)
Returns the contrast ratio between
foreground and background . |
static double |
calculateLuminance(int color)
Returns the luminance of a color as a float between
0.0 and 1.0 . |
static int |
calculateMinimumAlpha(int foreground,
int background,
float minContrastRatio)
Calculates the minimum alpha value which can be applied to
foreground so that would
have a contrast value of at least minContrastRatio when compared to
background . |
static void |
colorToHSL(int color,
float[] outHsl)
Convert the ARGB color to its HSL (hue-saturation-lightness) components.
|
static void |
colorToLAB(int color,
double[] outLab)
Convert the ARGB color to its CIE Lab representative components.
|
static void |
colorToXYZ(int color,
double[] outXyz)
Convert the ARGB color to it's CIE XYZ representative components.
|
static int |
compositeColors(int foreground,
int background)
Composite two potentially translucent colors over each other and returns the result.
|
static double |
distanceEuclidean(double[] labX,
double[] labY)
Returns the euclidean distance between two LAB colors.
|
static int |
HSLToColor(float[] hsl)
Convert HSL (hue-saturation-lightness) components to a RGB color.
|
static int |
LABToColor(double l,
double a,
double b)
Converts a color from CIE Lab to its RGB representation.
|
static void |
LABToXYZ(double l,
double a,
double b,
double[] outXyz)
Converts a color from CIE Lab to CIE XYZ representation.
|
static void |
RGBToHSL(int r,
int g,
int b,
float[] outHsl)
Convert RGB components to HSL (hue-saturation-lightness).
|
static void |
RGBToLAB(int r,
int g,
int b,
double[] outLab)
Convert RGB components to its CIE Lab representative components.
|
static void |
RGBToXYZ(int r,
int g,
int b,
double[] outXyz)
Convert RGB components to it's CIE XYZ representative components.
|
static int |
setAlphaComponent(int color,
int alpha)
Set the alpha component of
color to be alpha . |
static int |
XYZToColor(double x,
double y,
double z)
Converts a color from CIE XYZ to its RGB representation.
|
static void |
XYZToLAB(double x,
double y,
double z,
double[] outLab)
Converts a color from CIE XYZ to CIE Lab representation.
|
public static int compositeColors(int foreground, int background)
public static double calculateLuminance(int color)
0.0
and 1.0
.
Defined as the Y component in the XYZ representation of color
.
public static double calculateContrast(int foreground, int background)
foreground
and background
.
background
must be opaque.
Formula defined here.
public static int calculateMinimumAlpha(int foreground, int background, float minContrastRatio)
foreground
so that would
have a contrast value of at least minContrastRatio
when compared to
background
.foreground
- the foreground colorbackground
- the opaque background colorminContrastRatio
- the minimum contrast ratiopublic static void RGBToHSL(int r, int g, int b, @NonNull float[] outHsl)
r
- red component value [0..255]g
- green component value [0..255]b
- blue component value [0..255]outHsl
- 3-element array which holds the resulting HSL componentspublic static void colorToHSL(int color, @NonNull float[] outHsl)
color
- the ARGB color to convert. The alpha component is ignoredoutHsl
- 3-element array which holds the resulting HSL componentspublic static int HSLToColor(@NonNull float[] hsl)
hsl
- 3-element array which holds the input HSL componentspublic static int setAlphaComponent(int color, int alpha)
color
to be alpha
.public static void colorToLAB(int color, @NonNull double[] outLab)
color
- the ARGB color to convert. The alpha component is ignoredoutLab
- 3-element array which holds the resulting LAB componentspublic static void RGBToLAB(int r, int g, int b, @NonNull double[] outLab)
r
- red component value [0..255]g
- green component value [0..255]b
- blue component value [0..255]outLab
- 3-element array which holds the resulting LAB componentspublic static void colorToXYZ(int color, @NonNull double[] outXyz)
The resulting XYZ representation will use the D65 illuminant and the CIE 2° Standard Observer (1931).
color
- the ARGB color to convert. The alpha component is ignoredoutXyz
- 3-element array which holds the resulting LAB componentspublic static void RGBToXYZ(int r, int g, int b, @NonNull double[] outXyz)
The resulting XYZ representation will use the D65 illuminant and the CIE 2° Standard Observer (1931).
r
- red component value [0..255]g
- green component value [0..255]b
- blue component value [0..255]outXyz
- 3-element array which holds the resulting XYZ componentspublic static void XYZToLAB(double x, double y, double z, @NonNull double[] outLab)
This method expects the XYZ representation to use the D65 illuminant and the CIE 2° Standard Observer (1931).
x
- X component value [0...95.047)y
- Y component value [0...100)z
- Z component value [0...108.883)outLab
- 3-element array which holds the resulting Lab componentspublic static void LABToXYZ(double l, double a, double b, @NonNull double[] outXyz)
The resulting XYZ representation will use the D65 illuminant and the CIE 2° Standard Observer (1931).
l
- L component value [0...100)a
- A component value [-128...127)b
- B component value [-128...127)outXyz
- 3-element array which holds the resulting XYZ componentspublic static int XYZToColor(double x, double y, double z)
This method expects the XYZ representation to use the D65 illuminant and the CIE 2° Standard Observer (1931).
x
- X component value [0...95.047)y
- Y component value [0...100)z
- Z component value [0...108.883)public static int LABToColor(double l, double a, double b)
l
- L component value [0...100]a
- A component value [-128...127]b
- B component value [-128...127]public static double distanceEuclidean(@NonNull double[] labX, @NonNull double[] labY)
public static int blendARGB(int color1, int color2, float ratio)
A blend ratio of 0.0 will result in color1
, 0.5 will give an even blend,
1.0 will result in color2
.
color1
- the first ARGB colorcolor2
- the second ARGB colorratio
- the blend ratio of color1
to color2
public static void blendHSL(@NonNull float[] hsl1, @NonNull float[] hsl2, float ratio, @NonNull float[] outResult)
hsl1
and hsl2
using the given ratio. This will interpolate
the hue using the shortest angle.
A blend ratio of 0.0 will result in hsl1
, 0.5 will give an even blend,
1.0 will result in hsl2
.
hsl1
- 3-element array which holds the first HSL colorhsl2
- 3-element array which holds the second HSL colorratio
- the blend ratio of hsl1
to hsl2
outResult
- 3-element array which holds the resulting HSL componentspublic static void blendLAB(@NonNull double[] lab1, @NonNull double[] lab2, double ratio, @NonNull double[] outResult)
A blend ratio of 0.0 will result in lab1
, 0.5 will give an even blend,
1.0 will result in lab2
.
lab1
- 3-element array which holds the first LAB colorlab2
- 3-element array which holds the second LAB colorratio
- the blend ratio of lab1
to lab2
outResult
- 3-element array which holds the resulting LAB components