Class ColorModel
java.lang.Object
com.iizigo.prop.editor.color.swt.ColorModel
The color arithmetic of the SWT color chooser (CC-COLOR-CHOOSER-SWT-PANELS, 2026-10-10): the conversions between an ARGB value and the HSV, HSL and CMYK channel models, the hex notation of the chooser's hex field, and the SWT
RGB/RGBA forms.Pure functions without any widget or device dependency, so that every conversion is tested headless. Hue is in degrees [0,360), every other channel in [0,1]. A color is an int in the IIZI convention 0xAARRGGBB; the model conversions ignore the alpha byte and return an opaque color, the caller keeps the alpha it wants.
- Author:
- Christopher Mindus
Method Summary
Modifier and TypeMethodDescriptionstatic intalpha(int argb) Gets the alpha byte of a color, 255 for opaque.static intargb(int a, int r, int g, int b) Builds a color from its bytes, each clamped to0..255.static intblue(int argb) Gets the blue byte of a color.static intclampByte(int v) Clamps a value to a byte.static floatclampUnit(float v) Clamps a channel value to[0,1]; NaN becomes 0.static intcmykToRgb(float c, float m, float y, float k) Converts cyan, magenta, yellow and key to an opaque color.static intGets the opaque color of an SWT RGB.static intGets the color of an SWT RGBA.static intgreen(int argb) Gets the green byte of a color.static inthslToRgb(float h, float s, float l) Converts hue, saturation and lightness to an opaque color.static inthsvToRgb(float h, float s, float v) Converts hue, saturation and value to an opaque color.static floatnormalizeHue(float h) Normalizes a hue to[0,360); NaN becomes 0.static intopaque(int argb) Makes a color opaque: the alpha byte becomes 255, the RGB bytes are kept.static IntegerParses the hex field: six digitsRRGGBB(an opaque color) or, when alpha is allowed, eight digitsAARRGGBB; a leading#and surrounding blanks are accepted.static intred(int argb) Gets the red byte of a color.static intrgb(int r, int g, int b) Builds an opaque color from its bytes, each clamped to0..255.static float[]rgbToCmyk(int argb) Converts a color to cyan, magenta, yellow and key (black).static float[]rgbToHsl(int argb) Converts a color to hue, saturation and lightness.static float[]rgbToHsv(int argb) Converts a color to hue, saturation and value.static StringtoHex(int argb, boolean withAlpha) Formats a color for the hex field: six upper-case digitsRRGGBBfor an opaque color or when the alpha is not wanted, eight digitsAARRGGBBotherwise.static StringtooltipFor(int argb) Formats the tooltip of a color: the hex value and the RGB components, as#RRGGBB r,g,b.static RGBtoRGB(int argb) Gets the SWT RGB of a color (the alpha byte is dropped).static RGBAtoRGBA(int argb) Gets the SWT RGBA of a color.static intwithAlpha(int argb, int alpha) Replaces the alpha byte of a color.
Method Details
alpha
public static int alpha(int argb) Gets the alpha byte of a color, 255 for opaque.red
public static int red(int argb) Gets the red byte of a color.green
public static int green(int argb) Gets the green byte of a color.blue
public static int blue(int argb) Gets the blue byte of a color.argb
public static int argb(int a, int r, int g, int b) Builds a color from its bytes, each clamped to0..255.rgb
public static int rgb(int r, int g, int b) Builds an opaque color from its bytes, each clamped to0..255.opaque
public static int opaque(int argb) Makes a color opaque: the alpha byte becomes 255, the RGB bytes are kept.withAlpha
public static int withAlpha(int argb, int alpha) Replaces the alpha byte of a color.- Parameters:
argb- The color.alpha- The new alpha, clamped to0..255.
clampByte
public static int clampByte(int v) Clamps a value to a byte.clampUnit
public static float clampUnit(float v) Clamps a channel value to[0,1]; NaN becomes 0.normalizeHue
public static float normalizeHue(float h) Normalizes a hue to[0,360); NaN becomes 0.rgbToHsv
public static float[] rgbToHsv(int argb) Converts a color to hue, saturation and value.- Parameters:
argb- The color; the alpha byte is ignored.- Returns:
{h,s,v}with the hue in degrees[0,360)(0 for a gray) and the others in[0,1].
hsvToRgb
public static int hsvToRgb(float h, float s, float v) Converts hue, saturation and value to an opaque color.- Parameters:
h- The hue in degrees, any value (normalized).s- The saturation[0,1](clamped).v- The value[0,1](clamped).- Returns:
- The opaque color
0xFFRRGGBB.
rgbToHsl
public static float[] rgbToHsl(int argb) Converts a color to hue, saturation and lightness.- Parameters:
argb- The color; the alpha byte is ignored.- Returns:
{h,s,l}with the hue in degrees[0,360)(0 for a gray) and the others in[0,1].
hslToRgb
public static int hslToRgb(float h, float s, float l) Converts hue, saturation and lightness to an opaque color.- Parameters:
h- The hue in degrees, any value (normalized).s- The saturation[0,1](clamped).l- The lightness[0,1](clamped).- Returns:
- The opaque color
0xFFRRGGBB.
rgbToCmyk
public static float[] rgbToCmyk(int argb) Converts a color to cyan, magenta, yellow and key (black).- Parameters:
argb- The color; the alpha byte is ignored.- Returns:
{c,m,y,k}in[0,1]; black is{0,0,0,1}.
cmykToRgb
public static int cmykToRgb(float c, float m, float y, float k) Converts cyan, magenta, yellow and key to an opaque color.- Parameters:
c- Cyan[0,1](clamped).m- Magenta[0,1](clamped).y- Yellow[0,1](clamped).k- Key[0,1](clamped).- Returns:
- The opaque color
0xFFRRGGBB.
toHex
Formats a color for the hex field: six upper-case digitsRRGGBBfor an opaque color or when the alpha is not wanted, eight digitsAARRGGBBotherwise.- Parameters:
argb- The color.withAlpha- true to write the alpha byte when it is not 255.
tooltipFor
Formats the tooltip of a color: the hex value and the RGB components, as#RRGGBB r,g,b.parseHex
Parses the hex field: six digitsRRGGBB(an opaque color) or, when alpha is allowed, eight digitsAARRGGBB; a leading#and surrounding blanks are accepted.- Parameters:
text- The text.allowAlpha- true to accept eight digits.- Returns:
- The color, or null when the text is not a color.
toRGB
Gets the SWT RGB of a color (the alpha byte is dropped).toRGBA
Gets the SWT RGBA of a color.fromRGB
Gets the opaque color of an SWT RGB.fromRGBA
Gets the color of an SWT RGBA.