Module: Abachrome

Defined in:
lib/abachrome/converter.rb,
lib/abachrome.rb,
lib/abachrome/color.rb,
lib/abachrome/palette.rb,
lib/abachrome/to_abcd.rb,
lib/abachrome/version.rb,
lib/abachrome/gamut/p3.rb,
lib/abachrome/named/css.rb,
lib/abachrome/gamut/base.rb,
lib/abachrome/gamut/srgb.rb,
lib/abachrome/abc_decimal.rb,
lib/abachrome/color_space.rb,
lib/abachrome/outputs/css.rb,
lib/abachrome/parsers/css.rb,
lib/abachrome/parsers/hex.rb,
lib/abachrome/gamut/rec2020.rb,
lib/abachrome/named/tailwind.rb,
lib/abachrome/converters/base.rb,
lib/abachrome/illuminants/d50.rb,
lib/abachrome/illuminants/d55.rb,
lib/abachrome/illuminants/d65.rb,
lib/abachrome/illuminants/d75.rb,
lib/abachrome/color_models/hsv.rb,
lib/abachrome/color_models/lms.rb,
lib/abachrome/color_models/rgb.rb,
lib/abachrome/color_models/xyz.rb,
lib/abachrome/illuminants/base.rb,
lib/abachrome/parsers/tailwind.rb,
lib/abachrome/color_mixins/blend.rb,
lib/abachrome/color_models/oklab.rb,
lib/abachrome/color_models/oklch.rb,
lib/abachrome/color_mixins/lighten.rb,
lib/abachrome/color_mixins/to_lrgb.rb,
lib/abachrome/color_mixins/to_srgb.rb,
lib/abachrome/color_mixins/to_oklab.rb,
lib/abachrome/color_mixins/to_oklch.rb,
lib/abachrome/converters/lms_to_xyz.rb,
lib/abachrome/converters/xyz_to_lms.rb,
lib/abachrome/converters/lms_to_lrgb.rb,
lib/abachrome/converters/lms_to_srgb.rb,
lib/abachrome/converters/lrgb_to_xyz.rb,
lib/abachrome/converters/lrgb_to_srgb.rb,
lib/abachrome/converters/oklab_to_lms.rb,
lib/abachrome/converters/oklch_to_xyz.rb,
lib/abachrome/converters/srgb_to_lrgb.rb,
lib/abachrome/converters/xyz_to_oklab.rb,
lib/abachrome/palette_mixins/resample.rb,
lib/abachrome/converters/lrgb_to_oklab.rb,
lib/abachrome/converters/oklab_to_lrgb.rb,
lib/abachrome/converters/oklab_to_srgb.rb,
lib/abachrome/converters/oklch_to_lrgb.rb,
lib/abachrome/converters/oklch_to_srgb.rb,
lib/abachrome/converters/srgb_to_oklab.rb,
lib/abachrome/converters/srgb_to_oklch.rb,
lib/abachrome/converters/oklab_to_oklch.rb,
lib/abachrome/converters/oklch_to_oklab.rb,
lib/abachrome/color_mixins/to_colorspace.rb,
lib/abachrome/palette_mixins/interpolate.rb,
lib/abachrome/palette_mixins/stretch_luminance.rb

Overview

Abachrome::PaletteMixins::Interpolate - Color palette interpolation functionality

This mixin provides methods for interpolating between adjacent colors in a palette to create smooth color transitions and gradients. The interpolation process inserts new colors between existing palette colors by blending them at calculated intervals, creating smoother color progressions ideal for gradients, color ramps, and visual transitions.

Key features:

  • Insert specified number of interpolated colors between each adjacent color pair

  • Both non-destructive (interpolate) and destructive (interpolate!) variants

  • Uses color blending in the current color space for smooth transitions

  • Maintains original colors as anchor points in the interpolated result

  • High-precision decimal arithmetic for accurate color calculations

  • Preserves alpha values during interpolation process

The mixin includes both immutable methods that return new palette instances and mutable methods that modify the current palette object in place, providing flexibility for different use cases and performance requirements. Interpolation is essential for creating smooth color gradients and ensuring adequate color resolution in palette-based applications.

Defined Under Namespace

Modules: ColorMixins, ColorModels, Converters, Gamut, Illuminants, Named, Outputs, PaletteMixins, Parsers, ToAbcd Classes: AbcDecimal, Color, ColorSpace, Converter, Palette

Constant Summary collapse

VERSION =
"0.1.5"

Class Method Summary collapse

Class Method Details

.convert(color, to_space) ⇒ Abachrome::Color

Convert a color from its current color space to another color space.

Parameters:

  • color (Abachrome::Color)

    The color object to convert

  • to_space (Symbol, String)

    The destination color space identifier (e.g. :srgb, :oklch)

Returns:



158
159
160
# File 'lib/abachrome.rb', line 158

def convert(color, to_space)
  Converter.convert(color, to_space)
end

.create_color(space_name, *coordinates, alpha: 1.0) ⇒ Abachrome::Color

Creates a new color in the specified color space with given coordinates and alpha value.

Parameters:

  • space_name (Symbol, String)

    The name of the color space (e.g., :srgb, :oklch)

  • coordinates (Array<Numeric>)

    The color coordinates in the specified color space

  • alpha (Float) (defaults to: 1.0)

    The alpha (opacity) value of the color, defaults to 1.0 (fully opaque)

Returns:

  • (Abachrome::Color)

    A new Color object in the specified color space with the given coordinates



80
81
82
83
# File 'lib/abachrome.rb', line 80

def create_color(space_name, *coordinates, alpha: 1.0)
  space = ColorSpace.find(space_name)
  Color.new(space, coordinates, alpha)
end

.from_hex(hex_str) ⇒ Abachrome::Color

Creates a color object from a hexadecimal color code string.

“#RGB”, “#RRGGBB”, “RGB”, or “RRGGBB”, with or without the leading “#” character. Abachrome.from_hex(“#ff0000”) # => returns a red Color object Abachrome.from_hex(“00ff00”) # => returns a green Color object

Parameters:

  • hex_str (String)

    The hexadecimal color code string to parse. Can be in formats like

Returns:

  • (Abachrome::Color)

    A new Color object representing the parsed hexadecimal color.

See Also:



127
128
129
# File 'lib/abachrome.rb', line 127

def from_hex(hex_str)
  Parsers::Hex.parse(hex_str)
end

.from_name(color_name) ⇒ Abachrome::Color?

Creates a color object from a CSS color name.

Case-insensitive. nil if the color name is not recognized.

Parameters:

  • color_name (String)

    The CSS color name (e.g., ‘red’, ‘blue’, ‘cornflowerblue’).

Returns:

  • (Abachrome::Color, nil)

    A color object in the RGB color space if the name is valid,



137
138
139
140
141
142
# File 'lib/abachrome.rb', line 137

def from_name(color_name)
  rgb_values = Named::CSS::ColorNames[color_name.downcase]
  return nil unless rgb_values

  from_rgb(*rgb_values.map { |v| v / 255.0 })
end

.from_oklab(l, a, b, alpha = 1.0) ⇒ Abachrome::Color

Creates a color in the OKLAB color space.

Parameters:

  • l (Numeric)

    The lightness component (L) in the OKLAB color space, typically in range 0 to 1

  • a (Numeric)

    The green-red component (a) in the OKLAB color space

  • b (Numeric)

    The blue-yellow component (b) in the OKLAB color space

  • alpha (Float) (defaults to: 1.0)

    The alpha (opacity) value, ranging from 0.0 (transparent) to 1.0 (opaque), defaults to 1.0

Returns:



103
104
105
# File 'lib/abachrome.rb', line 103

def from_oklab(l, a, b, alpha = 1.0)
  Color.from_oklab(l, a, b, alpha)
end

.from_oklch(l, a, b, alpha = 1.0) ⇒ Abachrome::Color

Creates a new color from OKLCH color space values.

Parameters:

  • l (Numeric)

    The lightness value, typically in range 0-1

  • a (Numeric)

    The chroma (colorfulness) value

  • b (Numeric)

    The hue angle value in degrees (0-360)

  • alpha (Numeric) (defaults to: 1.0)

    The alpha (opacity) value, between 0-1 (default: 1.0)

Returns:

  • (Abachrome::Color)

    A new color object initialized with the given OKLCH values



114
115
116
# File 'lib/abachrome.rb', line 114

def from_oklch(l, a, b, alpha = 1.0)
  Color.from_oklch(l, a, b, alpha)
end

.from_rgb(r, g, b, alpha = 1.0) ⇒ Abachrome::Color

Creates a color object from RGB values.

Parameters:

  • r (Numeric)

    The red component value (typically 0-255 or 0.0-1.0)

  • g (Numeric)

    The green component value (typically 0-255 or 0.0-1.0)

  • b (Numeric)

    The blue component value (typically 0-255 or 0.0-1.0)

  • alpha (Float) (defaults to: 1.0)

    The alpha (opacity) component value (0.0-1.0), defaults to 1.0 (fully opaque)

Returns:

  • (Abachrome::Color)

    A new Color object initialized with the specified RGB values



92
93
94
# File 'lib/abachrome.rb', line 92

def from_rgb(r, g, b, alpha = 1.0)
  Color.from_rgb(r, g, b, alpha)
end

.parse(css_string) ⇒ Abachrome::Color?

Parses a CSS color string and returns a Color object.

Parameters:

  • css_string (String)

    The CSS color string to parse (e.g., “#ff0000”, “rgb(255, 0, 0)”, “red”)

Returns:



148
149
150
151
# File 'lib/abachrome.rb', line 148

def parse(css_string)
  require_relative "abachrome/parsers/css"
  Parsers::CSS.parse(css_string)
end

.register_color_space(name, &block) ⇒ Abachrome::ColorSpace

Register a new color space with the Abachrome library.

Parameters:

  • name (Symbol, String)

    The identifier for the color space being registered

  • block (Proc)

    A block that defines the color space properties and conversion rules

Returns:



167
168
169
# File 'lib/abachrome.rb', line 167

def register_color_space(name, &block)
  ColorSpace.register(name, &block)
end

.register_converter(from_space, to_space, converter) ⇒ void

This method returns an undefined value.

Register a new color space converter in the Abachrome system.

This method allows registering custom converters between color spaces. Converters are used to transform color representations from one color space to another.

Parameters:

  • from_space (Symbol, String)

    The source color space identifier

  • to_space (Symbol, String)

    The destination color space identifier

  • converter (#call)

    An object responding to #call that performs the conversion



181
182
183
# File 'lib/abachrome.rb', line 181

def register_converter(from_space, to_space, converter)
  Converter.register(from_space, to_space, converter)
end