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/floatify.rb,
lib/abachrome/gamut/p3.rb,
lib/abachrome/spectral.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/color_models/yiq.rb,
lib/abachrome/illuminants/base.rb,
lib/abachrome/parsers/tailwind.rb,
lib/abachrome/color_mixins/wcag.rb,
lib/abachrome/color_models/cmyk.rb,
lib/abachrome/color_mixins/blend.rb,
lib/abachrome/color_models/oklab.rb,
lib/abachrome/color_models/oklch.rb,
lib/abachrome/color_mixins/to_lms.rb,
lib/abachrome/color_mixins/to_xyz.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/color_mixins/harmonies.rb,
lib/abachrome/converters/lms_to_lrgb.rb,
lib/abachrome/converters/lms_to_srgb.rb,
lib/abachrome/converters/lrgb_to_lms.rb,
lib/abachrome/converters/lrgb_to_xyz.rb,
lib/abachrome/converters/srgb_to_lms.rb,
lib/abachrome/converters/srgb_to_xyz.rb,
lib/abachrome/converters/srgb_to_yiq.rb,
lib/abachrome/converters/xyz_to_lrgb.rb,
lib/abachrome/converters/xyz_to_srgb.rb,
lib/abachrome/converters/yiq_to_srgb.rb,
lib/abachrome/converters/cmyk_to_srgb.rb,
lib/abachrome/converters/lms_to_oklab.rb,
lib/abachrome/converters/lrgb_to_srgb.rb,
lib/abachrome/converters/oklab_to_lms.rb,
lib/abachrome/converters/oklab_to_xyz.rb,
lib/abachrome/converters/oklch_to_lms.rb,
lib/abachrome/converters/oklch_to_xyz.rb,
lib/abachrome/converters/srgb_to_cmyk.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/color_mixins/spectral_mix.rb,
lib/abachrome/color_mixins/to_grayscale.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, Spectral, ToAbcd Classes: AbcDecimal, Color, ColorSpace, Converter, Palette

Constant Summary collapse

VERSION =
"0.2.0"

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:



214
215
216
# File 'lib/abachrome.rb', line 214

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



85
86
87
88
# File 'lib/abachrome.rb', line 85

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

.from_cmyk(c, m, y, k, alpha = 1.0) ⇒ Abachrome::Color

Creates a color in the CMYK color space.

Parameters:

  • c (Numeric)

    The cyan component, typically in range 0 to 1 (or 0 to 100 for percentages)

  • m (Numeric)

    The magenta component, typically in range 0 to 1 (or 0 to 100 for percentages)

  • y (Numeric)

    The yellow component, typically in range 0 to 1 (or 0 to 100 for percentages)

  • k (Numeric)

    The key/black component, typically in range 0 to 1 (or 0 to 100 for percentages)

  • alpha (Float) (defaults to: 1.0)

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

Returns:



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

def from_cmyk(c, m, y, k, alpha = 1.0)
  Color.from_cmyk(c, m, y, k, 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:



132
133
134
# File 'lib/abachrome.rb', line 132

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,



142
143
144
145
146
147
# File 'lib/abachrome.rb', line 142

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:



108
109
110
# File 'lib/abachrome.rb', line 108

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



119
120
121
# File 'lib/abachrome.rb', line 119

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



97
98
99
# File 'lib/abachrome.rb', line 97

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

.from_yiq(y, i, q, alpha = 1.0) ⇒ Abachrome::Color

Creates a color in the YIQ color space.

Parameters:

  • y (Numeric)

    The luma (brightness) component, typically in range 0 to 1

  • i (Numeric)

    The in-phase component (orange-blue), typically in range -0.5957 to 0.5957

  • q (Numeric)

    The quadrature component (purple-green), typically in range -0.5226 to 0.5226

  • alpha (Float) (defaults to: 1.0)

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

Returns:



156
157
158
# File 'lib/abachrome.rb', line 156

def from_yiq(y, i, q, alpha = 1.0)
  Color.from_yiq(y, i, q, 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:



204
205
206
207
# File 'lib/abachrome.rb', line 204

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:



223
224
225
# File 'lib/abachrome.rb', line 223

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



237
238
239
# File 'lib/abachrome.rb', line 237

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

.spectral_mix(colors, tinting_strengths: {}) ⇒ Abachrome::Color

Mix multiple colors using Kubelka-Munk spectral mixing.

This function produces more realistic color mixing than simple RGB or LAB interpolation by simulating how real pigments absorb and scatter light. Based on spectral.js by Ronald van Wijnen (https://github.com/rvanwijnen/spectral.js)

Examples:

Mix red and blue with equal weights

red = Abachrome.from_rgb(1, 0, 0)
blue = Abachrome.from_rgb(0, 0, 1)
purple = Abachrome.spectral_mix([
  {color: red, weight: 1},
  {color: blue, weight: 1}
])

Mix three colors with different weights

mixed = Abachrome.spectral_mix([
  {color: red, weight: 2},
  {color: green, weight: 1},
  {color: blue, weight: 1}
])

Parameters:

  • colors (Array<Hash>)

    Array of hashes with :color (Abachrome::Color) and :weight (Numeric)

  • tinting_strengths (Hash) (defaults to: {})

    Optional hash mapping colors to tinting strengths (default: 1.0)

Returns:



196
197
198
# File 'lib/abachrome.rb', line 196

def spectral_mix(colors, tinting_strengths: {})
  Spectral.mix(colors, tinting_strengths: tinting_strengths)
end