module

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

BLACK = (Gray16.new(0_u16)).as(Color)

Standard colors

BLUE = RGBA.new(0, 0, 255, 255)
BROWN = RGBA.new(165, 42, 42, 255)
CYAN = RGBA.new(0, 255, 255, 255)
GRAY = RGBA.new(128, 128, 128, 255)
GREEN = RGBA.new(0, 255, 0, 255)
GREY = RGBA.new(128, 128, 128, 255)
MAGENTA = RGBA.new(255, 0, 255, 255)
MAX_16BIT = 65535_u16
MAX_32BIT = 65535_u32
MAX_8BIT = 255_u8

Color bit depth constants

OPAQUE = (Alpha16.new(MAX_16BIT)).as(Color)
ORANGE = RGBA.new(255, 165, 0, 255)
PINK = RGBA.new(255, 192, 203, 255)
PURPLE = RGBA.new(128, 0, 128, 255)
RED = RGBA.new(255, 0, 0, 255)

Common named colors (CSS/Web colors)

RGB_FROM_CB_COEFF = 116130
RGB_FROM_CB_G_COEFF = 22554
RGB_FROM_CR_COEFF = 91881

YCbCr to RGB conversion coefficients (scaled by 65536)

RGB_FROM_CR_G_COEFF = 46802
RGB_TO_CB_B_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_CB_B_COEFF * i.to_i32 end
RGB_TO_CB_G_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_CB_G_COEFF * i.to_i32 end
RGB_TO_CB_R_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_CB_R_COEFF * i.to_i32 end
RGB_TO_CR_B_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_CR_B_COEFF * i.to_i32 end
RGB_TO_CR_G_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_CR_G_COEFF * i.to_i32 end
RGB_TO_CR_R_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_CR_R_COEFF * i.to_i32 end
RGB_TO_Y_B_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_Y_B_COEFF * i.to_i32 end
RGB_TO_Y_G_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_Y_G_COEFF * i.to_i32 end
RGB_TO_Y_R_TABLE = StaticArray(Int32, 256).new do |i| YCBCR_Y_R_COEFF * i.to_i32 end
SCALE_8_TO_16 = 257_u32
TRANSPARENT = (Alpha16.new(0_u16)).as(Color)
WHITE = (Gray16.new(MAX_16BIT)).as(Color)
YCBCR_CB_B_COEFF = 32768
YCBCR_CB_B_TABLE = StaticArray(Int32, 256).new do |i| RGB_FROM_CB_COEFF * (i.to_i32 - 128) end
YCBCR_CB_G_COEFF = -21712
YCBCR_CB_G_TABLE = StaticArray(Int32, 256).new do |i| RGB_FROM_CB_G_COEFF * (i.to_i32 - 128) end
YCBCR_CB_R_COEFF = -11056

Cb/Cr conversion coefficients

YCBCR_CHROMA_OFFSET = 257 << 15

RGB to YCbCr rounding adjustment: 257 &lt;&lt; 15

YCBCR_CR_B_COEFF = -5328
YCBCR_CR_G_COEFF = -27440
YCBCR_CR_G_TABLE = StaticArray(Int32, 256).new do |i| RGB_FROM_CR_G_COEFF * (i.to_i32 - 128) end
YCBCR_CR_R_COEFF = 32768
YCBCR_CR_R_TABLE = StaticArray(Int32, 256).new do |i| RGB_FROM_CR_COEFF * (i.to_i32 - 128) end
YCBCR_RANGE_LIMIT = StaticArray(UInt8, 768).new do |i| value = i.to_i32 - 256 if value < 0 value = 0 end if value > 255 value = 255 end value.to_u8 end
YCBCR_Y_B_COEFF = 7471
YCBCR_Y_G_COEFF = 38470
YCBCR_Y_R_COEFF = 19595

YCbCr conversion constants (JFIF specification) Y' = 0.2990R + 0.5870G + 0.1140*B Note: 19595 + 38470 + 7471 = 65536

YCBCR_YY1_MULTIPLIER = 65793

YY1 multiplier for YCbCr to RGB: Y' * 0x10101

YCBCR_YY1_TABLE = StaticArray(Int32, 256).new do |i| i.to_i32 * YCBCR_YY1_MULTIPLIER end
YELLOW = RGBA.new(255, 255, 0, 255)

Class methods

alpha16_model
Source
alpha_model
Source
blend(c1 : Color, c2 : Color, ratio : Float64) : RGBA

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) # =&gt; Purple
Source
clamp_ycbcr_16bit(value : Int32) : UInt32
Source
clamp_ycbcr_8bit(value : Int32) : UInt8
Source
cmyk_model
Source
cmyk_to_rgb(c : UInt8, m : UInt8, y : UInt8, k : UInt8) : Tuple(UInt8, UInt8, UInt8)

converts a CMYK quadruple to an RGB triple.

Source
contrasting(color : Color) : RGBA

Get a contrasting color (black or white) for text on this background.

text_color = Color.contrasting(background_color)
Source
dark?(color : Color) : Bool

Check if a color is considered &quot;dark&quot; (luminance &lt;= 0.5).

Color.dark?(Color::BLACK) # =&gt; true
Color.dark?(Color::WHITE) # =&gt; false
Source
darken(color : Color, amount : Float64) : RGBA

Darken a color by a percentage (0.0 to 1.0).

darker = Color.darken(Color::RED, 0.2) # =&gt; Darker red
Source
distance(c1 : Color, c2 : Color) : Float64

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) # =&gt; ~360.62
Source
gray(value : Int) : Gray

Create grayscale color

Color.gray(128) # =&gt; 50% gray
Source
gray16_model
Source
gray_model
Source
hex(value : Int, has_alpha : Bool = true) : RGBA

Create RGBA color from hex integer

Color.hex(0xFF0000FF) # =&gt; Red with full alpha
Color.hex(0xFF0000)   # =&gt; Red (alpha = 255)
Source
interpolate(c1 : Color, c2 : Color, t : Float64) : RGBA

Alias for blend - interpolate between two colors.

mid = Color.interpolate(Color::RED, Color::BLUE, 0.5) # =&gt; Purple
Source
light?(color : Color) : Bool

Check if a color is considered &quot;light&quot; (luminance &gt; 0.5). Useful for determining text color on backgrounds.

Color.light?(Color::WHITE) # =&gt; true
Color.light?(Color::BLACK) # =&gt; false
Source
lighten(color : Color, amount : Float64) : RGBA

Lighten a color by a percentage (0.0 to 1.0).

lighter = Color.lighten(Color::RED, 0.2) # =&gt; Lighter red
Source
luminance(color : Color) : Float64

Calculate perceived luminance of a color (0.0 to 1.0). Uses standard luminance coefficients (ITU-R BT.709).

lum = Color.luminance(Color::WHITE) # =&gt; 1.0
lum = Color.luminance(Color::BLACK) # =&gt; 0.0
Source
mix(colors : Array(Color)) : RGBA

Mix multiple colors together (average).

mixed = Color.mix([Color::RED, Color::GREEN, Color::BLUE])
Source
nrgba64_model
Source
nrgba_model
Source
nycbcra_model
Source
parse(str : String) : RGBA

Parse a color from various string formats:

  • Hex: &quot;#RRGGBB&quot;, &quot;#RRGGBBAA&quot;, &quot;#RGB&quot;, &quot;#RGBA&quot;
  • CSS: &quot;rgb(r, g, b)&quot;, &quot;rgba(r, g, b, a)&quot;
  • Named: &quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, etc.

Examples:

Color.parse(&quot;#FF0000&quot;)            # =&gt; Red
Color.parse(&quot;#FF0000FF&quot;)          # =&gt; Red with alpha
Color.parse(&quot;#F00&quot;)               # =&gt; Red (short form)
Color.parse(&quot;rgb(255, 0, 0)&quot;)     # =&gt; Red
Color.parse(&quot;rgba(255, 0, 0, 1)&quot;) # =&gt; Red with alpha
Color.parse(&quot;red&quot;)                # =&gt; Red
Source
rgb(r : Int, g : Int, b : Int) : RGBA

Create RGB color (alpha = 255)

Color.rgb(255, 0, 0) # =&gt; Red
Source
rgb_to_cmyk(r : UInt8, g : UInt8, b : UInt8) : Tuple(UInt8, UInt8, UInt8, UInt8)

converts an RGB triple to a CMYK quadruple.

Source
rgb_to_ycbcr(r : UInt8, g : UInt8, b : UInt8) : Tuple(UInt8, UInt8, UInt8)

converts an RGB triple to a Y'CbCr triple.

Source
rgba(r : Int, g : Int, b : Int, a : Int) : RGBA

Create RGBA color

Color.rgba(255, 0, 0, 128) # =&gt; Semi-transparent red
Source
rgba64_model
Source
rgba_model
Source
with_alpha(color : Color, alpha : Int) : RGBA

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) # =&gt; Semi-transparent red
Source
ycbcr_model
Source
ycbcr_to_rgb(y : UInt8, cb : UInt8, cr : UInt8) : Tuple(UInt8, UInt8, UInt8)
Source
ycbcr_to_rgb_16bit(y : UInt8, cb : UInt8, cr : UInt8) : Tuple(UInt32, UInt32, UInt32)

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

Source

Nested types