CrImage::Color
Module Color implements a basic color library Color is an interface that defines the minimal method set of any type that can be considered a color: one that can be converted to red, green, blue and alpha values. The conversion may be lossy, such as converting from CMYK or YCbCr color spaces.
Constants
Standard colors
Color bit depth constants
Common named colors (CSS/Web colors)
YCbCr to RGB conversion coefficients (scaled by 65536)
Cb/Cr conversion coefficients
RGB to YCbCr rounding adjustment: 257 << 15
YCbCr conversion constants (JFIF specification) Y' = 0.2990R + 0.5870G + 0.1140*B Note: 19595 + 38470 + 7471 = 65536
YY1 multiplier for YCbCr to RGB: Y' * 0x10101
Class methods
Blend two colors together with a ratio. Ratio of 0.0 returns c1, ratio of 1.0 returns c2.
blended = Color.blend(Color::RED, Color::BLUE, 0.5) # => Purple
converts a CMYK quadruple to an RGB triple.
Get a contrasting color (black or white) for text on this background.
text_color = Color.contrasting(background_color)
Check if a color is considered "dark" (luminance <= 0.5).
Color.dark?(Color::BLACK) # => true
Color.dark?(Color::WHITE) # => false
Darken a color by a percentage (0.0 to 1.0).
darker = Color.darken(Color::RED, 0.2) # => Darker red
Calculate Euclidean distance between two colors in RGB space. Returns a value between 0.0 (identical) and ~441.67 (black to white).
dist = Color.distance(Color::RED, Color::BLUE) # => ~360.62
Create RGBA color from hex integer
Color.hex(0xFF0000FF) # => Red with full alpha
Color.hex(0xFF0000) # => Red (alpha = 255)
Alias for blend - interpolate between two colors.
mid = Color.interpolate(Color::RED, Color::BLUE, 0.5) # => Purple
Check if a color is considered "light" (luminance > 0.5). Useful for determining text color on backgrounds.
Color.light?(Color::WHITE) # => true
Color.light?(Color::BLACK) # => false
Lighten a color by a percentage (0.0 to 1.0).
lighter = Color.lighten(Color::RED, 0.2) # => Lighter red
Calculate perceived luminance of a color (0.0 to 1.0). Uses standard luminance coefficients (ITU-R BT.709).
lum = Color.luminance(Color::WHITE) # => 1.0
lum = Color.luminance(Color::BLACK) # => 0.0
Mix multiple colors together (average).
mixed = Color.mix([Color::RED, Color::GREEN, Color::BLUE])
Parse a color from various string formats:
- Hex: "#RRGGBB", "#RRGGBBAA", "#RGB", "#RGBA"
- CSS: "rgb(r, g, b)", "rgba(r, g, b, a)"
- Named: "red", "blue", "green", etc.
Examples:
Color.parse("#FF0000") # => Red
Color.parse("#FF0000FF") # => Red with alpha
Color.parse("#F00") # => Red (short form)
Color.parse("rgb(255, 0, 0)") # => Red
Color.parse("rgba(255, 0, 0, 1)") # => Red with alpha
Color.parse("red") # => Red
Create RGB color (alpha = 255)
Color.rgb(255, 0, 0) # => Red
converts an RGB triple to a CMYK quadruple.
converts an RGB triple to a Y'CbCr triple.
Create RGBA color
Color.rgba(255, 0, 0, 128) # => Semi-transparent red
Create a new color with a different alpha value. Alpha should be 0-255 for 8-bit or 0-65535 for 16-bit.
semi_red = Color.with_alpha(Color::RED, 128) # => Semi-transparent red
Helper method to convert YCbCr values to RGB components (16-bit) Returns RGB values in 16-bit range (0-65535) This is used internally for fast YCbCr to RGB conversion
Nested types
- CrImage::Color::Alpha
- CrImage::Color::Alpha16
- CrImage::Color::CMYK
- CrImage::Color::Color
- CrImage::Color::Gray
- CrImage::Color::Gray16
- CrImage::Color::HSL
- CrImage::Color::HSV
- CrImage::Color::LAB
- CrImage::Color::Model
- CrImage::Color::ModelProc
- CrImage::Color::NRGBA
- CrImage::Color::NRGBA64
- CrImage::Color::NYCbCrA
- CrImage::Color::Palette
- CrImage::Color::RGBA
- CrImage::Color::RGBA64
- CrImage::Color::YCbCr