module RatatuiRuby::Style::Color
Color constructors for creating RGB color values.
Styles accept colors in multiple formats: symbols (:red), indexed integers (0-255), or hex strings ("#FF0000"). Converting from other color representations manually is tedious.
This module provides factory methods matching Ratatui’s Color API. Convert from hex integers, HSL, or other formats in a single call.
Use these constructors when you have color data in non-standard formats.
Example
# From a hex integer (common in design tools) red = Style::Color.from_u32(0xFF0000) style = Style::Style.new(fg: red) # From HSL (common in color pickers) blue = Style::Color.from_hsl(240, 100, 50) style = Style::Style.new(bg: blue)
Public Class Methods
Source
# File lib/ratatui_ruby/style/color.rb, line 101 def from_hsl(h, s, l) RatatuiRuby._color_from_hsl(h.to_f, s.to_f, l.to_f) end
Creates a color from HSL (Hue, Saturation, Lightness) values.
Color pickers often use HSL because it matches human perception better than RGB. Converting HSL to RGB manually requires math.
This method handles the conversion by delegating to Ratatui’s palette integration.
Note: This implementation uses degrees (0-360) for hue and percentages (0-100) for saturation and lightness, matching common UI conventions.
Example
Color.from_hsl(0, 100, 50) # => "#ff0000" (red) Color.from_hsl(120, 100, 50) # => "#00ff00" (green) Color.from_hsl(240, 100, 50) # => "#0000ff" (blue) Color.from_hsl(0, 0, 50) # => "#808080" (gray)
- h
-
Hue in degrees (0-360). Values wrap automatically.
- s
-
Saturation as percentage (0-100).
- l
-
Lightness as percentage (0-100).
Returns a hex string like "#rrggbb".
Source
# File lib/ratatui_ruby/style/color.rb, line 141 def from_hsluv(h, s, l) RatatuiRuby._color_from_hsluv(h.to_f, s.to_f, l.to_f) end
Creates a color from HSLuv (Human-friendly Hue, Saturation, Lightness) values.
HSLuv is a perceptually uniform color space. Unlike standard HSL, colors at the same lightness appear equally bright regardless of hue. This makes it ideal for generating color palettes with consistent perceived brightness.
This method delegates to Ratatui’s palette integration for the complex HSLuv to RGB conversion.
Note: Ratatui uses the range [-180, 180] for hue and [0, 100] for saturation and lightness. This implementation matches those conventions.
Example
Color.from_hsluv(12.18, 100, 53.2) # => "#ff0000" (bright red) Color.from_hsluv(-94.13, 100, 32.3) # => "#0000ff" (bright blue) Color.from_hsluv(0, 0, 50) # => gray
- h
-
Hue in degrees (-180 to 360). Values wrap automatically.
- s
-
Saturation as percentage (0-100). Values are clamped.
- l
-
Lightness as percentage (0-100). Values are clamped.
Returns a hex string like "#rrggbb".
Source
# File lib/ratatui_ruby/style/color.rb, line 65 def from_u32(value) RatatuiRuby._color_from_u32(value) end
Creates a color from a 32-bit unsigned integer.
Design tools and APIs often represent colors as hex integers. Manually extracting RGB components and formatting is error-prone.
This method parses the integer and returns a hex string ready for use with Style.
Example
Color.from_u32(0xFF0000) # => "#ff0000" (red) Color.from_u32(0x00FF00) # => "#00ff00" (green) Color.from_u32(0x0000FF) # => "#0000ff" (blue)
- value
-
Integer in
0xRRGGBBformat.
Returns a hex string like "#rrggbb".