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
-
.convert(color, to_space) ⇒ Abachrome::Color
Convert a color from its current color space to another color space.
-
.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.
-
.from_cmyk(c, m, y, k, alpha = 1.0) ⇒ Abachrome::Color
Creates a color in the CMYK color space.
-
.from_hex(hex_str) ⇒ Abachrome::Color
Creates a color object from a hexadecimal color code string.
-
.from_name(color_name) ⇒ Abachrome::Color?
Creates a color object from a CSS color name.
-
.from_oklab(l, a, b, alpha = 1.0) ⇒ Abachrome::Color
Creates a color in the OKLAB color space.
-
.from_oklch(l, a, b, alpha = 1.0) ⇒ Abachrome::Color
Creates a new color from OKLCH color space values.
-
.from_rgb(r, g, b, alpha = 1.0) ⇒ Abachrome::Color
Creates a color object from RGB values.
-
.from_yiq(y, i, q, alpha = 1.0) ⇒ Abachrome::Color
Creates a color in the YIQ color space.
-
.parse(css_string) ⇒ Abachrome::Color?
Parses a CSS color string and returns a Color object.
-
.register_color_space(name, &block) ⇒ Abachrome::ColorSpace
Register a new color space with the Abachrome library.
-
.register_converter(from_space, to_space, converter) ⇒ void
Register a new color space converter in the Abachrome system.
-
.spectral_mix(colors, tinting_strengths: {}) ⇒ Abachrome::Color
Mix multiple colors using Kubelka-Munk spectral mixing.
Class Method Details
.convert(color, to_space) ⇒ Abachrome::Color
Convert a color from its current color space to another color space.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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)
196 197 198 |
# File 'lib/abachrome.rb', line 196 def spectral_mix(colors, tinting_strengths: {}) Spectral.mix(colors, tinting_strengths: tinting_strengths) end |