public class

ColorUtils

extends Object
java.lang.Object
   ↳ lineageos.util.ColorUtils

Class Overview

Helper class for colorspace conversions, and color-related algorithms which may be generally useful.

Summary

Public Constructors
ColorUtils()
Public Methods
static double calculateDeltaE(double L1, double a1, double b1, double L2, double a2, double b2)
Calculate the colour difference value between two colours in lab space.
static float[] convertRGBtoLAB(int rgb)
Converts an RGB packed int into L*a*b space, which is well-suited for finding perceptual differences in color
static int dropAlpha(int rgba)
Drop the alpha component from an RGBA packed int and return a non sign-extended RGB int.
static int findPerceptuallyNearestColor(int rgb, int[] colors)
Finds the "perceptually nearest" color from a list of colors to the given RGB value.
static int findPerceptuallyNearestSolidColor(int rgb)
Convenience method to find the nearest "solid" color (having RGB components of either 0 or 255) to the given color.
static int generateAlertColorFromDrawable(Drawable drawable)
Takes a drawable and uses Palette to generate a suitable "alert" color which can be used for an external notification mechanism such as an RGB LED.
static Palette.Swatch getDominantSwatch(Palette palette)
Given a Palette, pick out the dominant swatch based on population
static float[] temperatureToRGB(int degreesK)
Convert a color temperature value (in Kelvin) to a RGB units as floats.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public ColorUtils ()

added in API level 5

Public Methods

public static double calculateDeltaE (double L1, double a1, double b1, double L2, double a2, double b2)

added in API level 5

Calculate the colour difference value between two colours in lab space. This code is from OpenIMAJ under BSD License

Parameters
L1 double: first colour's L component
a1 double: first colour's a component
b1 double: first colour's b component
L2 double: second colour's L component
a2 double: second colour's a component
b2 double: second colour's b component
Returns
double the CIE 2000 colour difference

public static float[] convertRGBtoLAB (int rgb)

added in API level 5

Converts an RGB packed int into L*a*b space, which is well-suited for finding perceptual differences in color

Parameters
rgb int: A 32-bit value of packed RGB ints
Returns
float[] array of Lab values of size 3

public static int dropAlpha (int rgba)

added in API level 5

Drop the alpha component from an RGBA packed int and return a non sign-extended RGB int.

Returns
int rgb

public static int findPerceptuallyNearestColor (int rgb, int[] colors)

added in API level 5

Finds the "perceptually nearest" color from a list of colors to the given RGB value. This is done by converting to L*a*b colorspace and using the CIE2000 deltaE algorithm.

Parameters
rgb int: The original color to start with
colors int: An array of colors to test
Returns
int RGB packed int of nearest color in the list

public static int findPerceptuallyNearestSolidColor (int rgb)

added in API level 5

Convenience method to find the nearest "solid" color (having RGB components of either 0 or 255) to the given color. This is useful for cases such as LED notification lights which may not be able to display the full range of colors due to hardware limitations.

Returns
int the perceptually nearest color in RGB

public static int generateAlertColorFromDrawable (Drawable drawable)

added in API level 5

Takes a drawable and uses Palette to generate a suitable "alert" color which can be used for an external notification mechanism such as an RGB LED. This will always pick a solid color having RGB components of 255 or 0.

Parameters
drawable Drawable: The drawable to generate a color for
Returns
int a suitable solid color which corresponds to the image

public static Palette.Swatch getDominantSwatch (Palette palette)

added in API level 8

Given a Palette, pick out the dominant swatch based on population

Returns
Palette.Swatch the dominant Swatch

public static float[] temperatureToRGB (int degreesK)

added in API level 5

Convert a color temperature value (in Kelvin) to a RGB units as floats. This can be used in a transform matrix or hardware gamma control.

Returns
float[] array of floats representing rgb values 0->1