abachrome 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/abachrome.gemspec +1 -0
- data/devenv.nix +1 -1
- data/lib/abachrome/abc_decimal.rb +38 -35
- data/lib/abachrome/color.rb +37 -10
- data/lib/abachrome/color_mixins/blend.rb +7 -5
- data/lib/abachrome/color_mixins/lighten.rb +8 -6
- data/lib/abachrome/color_mixins/spectral_mix.rb +70 -0
- data/lib/abachrome/color_mixins/to_colorspace.rb +10 -8
- data/lib/abachrome/color_mixins/to_grayscale.rb +87 -0
- data/lib/abachrome/color_mixins/to_lrgb.rb +14 -12
- data/lib/abachrome/color_mixins/to_oklab.rb +16 -14
- data/lib/abachrome/color_mixins/to_oklch.rb +12 -10
- data/lib/abachrome/color_mixins/to_srgb.rb +17 -15
- data/lib/abachrome/color_models/cmyk.rb +159 -0
- data/lib/abachrome/color_models/hsv.rb +5 -3
- data/lib/abachrome/color_models/lms.rb +3 -1
- data/lib/abachrome/color_models/oklab.rb +3 -1
- data/lib/abachrome/color_models/oklch.rb +6 -4
- data/lib/abachrome/color_models/rgb.rb +4 -2
- data/lib/abachrome/color_models/xyz.rb +3 -1
- data/lib/abachrome/color_models/yiq.rb +37 -0
- data/lib/abachrome/color_space.rb +28 -14
- data/lib/abachrome/converter.rb +10 -8
- data/lib/abachrome/converters/base.rb +13 -11
- data/lib/abachrome/converters/cmyk_to_srgb.rb +42 -0
- data/lib/abachrome/converters/lms_to_lrgb.rb +5 -3
- data/lib/abachrome/converters/lms_to_srgb.rb +6 -4
- data/lib/abachrome/converters/lms_to_xyz.rb +5 -3
- data/lib/abachrome/converters/lrgb_to_lms.rb +3 -1
- data/lib/abachrome/converters/lrgb_to_oklab.rb +5 -3
- data/lib/abachrome/converters/lrgb_to_srgb.rb +6 -4
- data/lib/abachrome/converters/lrgb_to_xyz.rb +5 -3
- data/lib/abachrome/converters/oklab_to_lms.rb +9 -7
- data/lib/abachrome/converters/oklab_to_lrgb.rb +7 -7
- data/lib/abachrome/converters/oklab_to_oklch.rb +4 -2
- data/lib/abachrome/converters/oklab_to_srgb.rb +4 -2
- data/lib/abachrome/converters/oklch_to_lrgb.rb +5 -3
- data/lib/abachrome/converters/oklch_to_oklab.rb +5 -3
- data/lib/abachrome/converters/oklch_to_srgb.rb +6 -4
- data/lib/abachrome/converters/oklch_to_xyz.rb +6 -4
- data/lib/abachrome/converters/srgb_to_cmyk.rb +64 -0
- data/lib/abachrome/converters/srgb_to_lrgb.rb +5 -3
- data/lib/abachrome/converters/srgb_to_oklab.rb +4 -2
- data/lib/abachrome/converters/srgb_to_oklch.rb +5 -3
- data/lib/abachrome/converters/srgb_to_yiq.rb +49 -0
- data/lib/abachrome/converters/xyz_to_lms.rb +5 -3
- data/lib/abachrome/converters/xyz_to_oklab.rb +5 -3
- data/lib/abachrome/converters/yiq_to_srgb.rb +47 -0
- data/lib/abachrome/gamut/base.rb +3 -3
- data/lib/abachrome/gamut/p3.rb +3 -3
- data/lib/abachrome/gamut/rec2020.rb +2 -2
- data/lib/abachrome/gamut/srgb.rb +4 -2
- data/lib/abachrome/illuminants/base.rb +2 -2
- data/lib/abachrome/illuminants/d50.rb +2 -2
- data/lib/abachrome/illuminants/d55.rb +2 -2
- data/lib/abachrome/illuminants/d65.rb +2 -2
- data/lib/abachrome/illuminants/d75.rb +2 -2
- data/lib/abachrome/named/css.rb +149 -149
- data/lib/abachrome/named/tailwind.rb +265 -265
- data/lib/abachrome/outputs/css.rb +2 -2
- data/lib/abachrome/palette.rb +26 -25
- data/lib/abachrome/palette_mixins/interpolate.rb +3 -1
- data/lib/abachrome/palette_mixins/resample.rb +2 -2
- data/lib/abachrome/palette_mixins/stretch_luminance.rb +2 -2
- data/lib/abachrome/parsers/css.rb +86 -71
- data/lib/abachrome/parsers/hex.rb +2 -2
- data/lib/abachrome/parsers/tailwind.rb +8 -8
- data/lib/abachrome/spectral.rb +277 -0
- data/lib/abachrome/to_abcd.rb +4 -4
- data/lib/abachrome/version.rb +2 -2
- data/lib/abachrome.rb +66 -10
- metadata +29 -3
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::ColorModels::Oklch - OKLCH color space model definition
|
|
2
4
|
#
|
|
3
5
|
# This module defines the OKLCH color model within the Abachrome color manipulation library.
|
|
@@ -23,7 +25,7 @@ module Abachrome
|
|
|
23
25
|
module ColorModels
|
|
24
26
|
class Oklch
|
|
25
27
|
# Normalizes OKLCH color values to their standard ranges.
|
|
26
|
-
#
|
|
28
|
+
#
|
|
27
29
|
# @param l [Numeric] The lightness component, will be clamped to range 0-1
|
|
28
30
|
# @param c [Numeric] The chroma component, will be clamped to range 0-1
|
|
29
31
|
# @param h [Numeric] The hue component in degrees, will be normalized to range 0-360
|
|
@@ -45,7 +47,7 @@ module Abachrome
|
|
|
45
47
|
end
|
|
46
48
|
|
|
47
49
|
# Converts OKLCH color coordinates to OKLab color coordinates.
|
|
48
|
-
#
|
|
50
|
+
#
|
|
49
51
|
# @param l [Numeric] The lightness value in the OKLCH color space
|
|
50
52
|
# @param c [Numeric] The chroma value in the OKLCH color space
|
|
51
53
|
# @param h [Numeric] The hue value in degrees in the OKLCH color space
|
|
@@ -59,7 +61,7 @@ module Abachrome
|
|
|
59
61
|
end
|
|
60
62
|
|
|
61
63
|
# Converts OKLab color coordinates to OKLCH color coordinates.
|
|
62
|
-
#
|
|
64
|
+
#
|
|
63
65
|
# @param l [Numeric] The lightness component from OKLab.
|
|
64
66
|
# @param a [Numeric] The green-red component from OKLab.
|
|
65
67
|
# @param b [Numeric] The blue-yellow component from OKLab.
|
|
@@ -86,4 +88,4 @@ ColorSpace.register(
|
|
|
86
88
|
["ok-lch"]
|
|
87
89
|
)
|
|
88
90
|
|
|
89
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
91
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::ColorModels::RGB - RGB color space model utilities
|
|
2
4
|
#
|
|
3
5
|
# This module provides utility methods for the RGB color model within the Abachrome
|
|
@@ -22,7 +24,7 @@ module Abachrome
|
|
|
22
24
|
class RGB
|
|
23
25
|
class << self
|
|
24
26
|
# Normalizes RGB color component values to the [0,1] range.
|
|
25
|
-
#
|
|
27
|
+
#
|
|
26
28
|
# @param r [String, Numeric] Red component. If string with % suffix, interpreted as percentage;
|
|
27
29
|
# if string without suffix or numeric > 1, interpreted as 0-255 range value;
|
|
28
30
|
# if numeric ≤ 1, used directly.
|
|
@@ -53,4 +55,4 @@ module Abachrome
|
|
|
53
55
|
end
|
|
54
56
|
end
|
|
55
57
|
|
|
56
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
58
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::ColorModels::Xyz - XYZ color space model definition
|
|
2
4
|
#
|
|
3
5
|
# This module defines the XYZ color model within the Abachrome color manipulation library.
|
|
@@ -26,4 +28,4 @@ module Abachrome
|
|
|
26
28
|
end
|
|
27
29
|
end
|
|
28
30
|
|
|
29
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
31
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Abachrome::ColorModels::YIQ - YIQ color space model utilities
|
|
4
|
+
#
|
|
5
|
+
# This module provides utility methods for the YIQ color model within the Abachrome
|
|
6
|
+
# color manipulation library. YIQ is a color space historically used by the NTSC
|
|
7
|
+
# television standard, separating luminance (Y) from chrominance (I and Q).
|
|
8
|
+
#
|
|
9
|
+
# Key features:
|
|
10
|
+
# - Separates brightness (luma) from color information
|
|
11
|
+
# - Y (luma): Weighted sum of RGB based on human eye sensitivity
|
|
12
|
+
# - I (In-phase): Orange-to-blue axis
|
|
13
|
+
# - Q (Quadrature): Purple-to-green axis
|
|
14
|
+
# - Supports legacy broadcast standards and computer vision applications
|
|
15
|
+
# - Uses Rec. 601 coefficients for luma calculation
|
|
16
|
+
#
|
|
17
|
+
# The YIQ model is essential for image processing algorithms that need to separate
|
|
18
|
+
# luminance from chrominance, including JPEG compression, grayscale conversion, and
|
|
19
|
+
# edge detection in computer vision applications.
|
|
20
|
+
|
|
21
|
+
module Abachrome
|
|
22
|
+
module ColorModels
|
|
23
|
+
class YIQ
|
|
24
|
+
class << self
|
|
25
|
+
# Normalizes YIQ color component values to their standard ranges.
|
|
26
|
+
#
|
|
27
|
+
# @param y [Numeric] Luma component (brightness), typically in range [0,1]
|
|
28
|
+
# @param i [Numeric] In-phase component (orange-blue), typically in range [-0.5957, 0.5957]
|
|
29
|
+
# @param q [Numeric] Quadrature component (purple-green), typically in range [-0.5226, 0.5226]
|
|
30
|
+
# @return [Array<AbcDecimal>] Array of three normalized components as AbcDecimal objects
|
|
31
|
+
def normalize(y, i, q)
|
|
32
|
+
[y, i, q].map { |value| AbcDecimal(value) }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::ColorSpace - Core color space definition and registry system
|
|
2
4
|
#
|
|
3
5
|
# This module provides the foundation for managing color spaces within the Abachrome library.
|
|
@@ -22,14 +24,14 @@ module Abachrome
|
|
|
22
24
|
class ColorSpace
|
|
23
25
|
class << self
|
|
24
26
|
# A registry of all registered color spaces.
|
|
25
|
-
#
|
|
27
|
+
#
|
|
26
28
|
# @return [Hash] A memoized hash where keys are color space identifiers and values are the corresponding color space objects
|
|
27
29
|
def registry
|
|
28
30
|
@registry ||= {}
|
|
29
31
|
end
|
|
30
32
|
|
|
31
33
|
# Registers a new color space with the specified name.
|
|
32
|
-
#
|
|
34
|
+
#
|
|
33
35
|
# @param name [String, Symbol] The identifier for the color space
|
|
34
36
|
# @param block [Proc] A block that configures the color space properties
|
|
35
37
|
# @return [Abachrome::ColorSpace] The newly created color space instance added to the registry
|
|
@@ -38,10 +40,10 @@ module Abachrome
|
|
|
38
40
|
end
|
|
39
41
|
|
|
40
42
|
# Aliases a color space name to an existing registered color space.
|
|
41
|
-
#
|
|
43
|
+
#
|
|
42
44
|
# This method creates an alias for an existing color space in the registry,
|
|
43
45
|
# allowing the same color space to be accessed through multiple names.
|
|
44
|
-
#
|
|
46
|
+
#
|
|
45
47
|
# @param name [Symbol, String] The existing color space name already registered
|
|
46
48
|
# @param aliased_name [Symbol, String] The new alias name to register
|
|
47
49
|
# @return [void]
|
|
@@ -60,7 +62,7 @@ module Abachrome
|
|
|
60
62
|
attr_reader :name, :coordinates, :white_point, :color_model
|
|
61
63
|
|
|
62
64
|
# Initialize a new ColorSpace instance.
|
|
63
|
-
#
|
|
65
|
+
#
|
|
64
66
|
# @param name [String, Symbol] The name of the color space, which will be converted to a symbol
|
|
65
67
|
# @return [Abachrome::ColorSpace] A new instance of ColorSpace
|
|
66
68
|
# @yield [self] Yields self to the block for configuration if a block is given
|
|
@@ -70,7 +72,7 @@ module Abachrome
|
|
|
70
72
|
end
|
|
71
73
|
|
|
72
74
|
# Sets the color coordinates for the current color space.
|
|
73
|
-
#
|
|
75
|
+
#
|
|
74
76
|
# @param [Array] coords The coordinate values that define a color in this color space.
|
|
75
77
|
# Multiple arguments or a single flat array can be provided.
|
|
76
78
|
# @return [Array] The flattened array of coordinates.
|
|
@@ -79,10 +81,10 @@ module Abachrome
|
|
|
79
81
|
end
|
|
80
82
|
|
|
81
83
|
# Sets the white point reference used by the color space.
|
|
82
|
-
#
|
|
84
|
+
#
|
|
83
85
|
# The white point is a reference that defines what is considered "white" in a color space.
|
|
84
86
|
# Common values include :D50, :D65, etc.
|
|
85
|
-
#
|
|
87
|
+
#
|
|
86
88
|
# @param point [Symbol, String] The white point reference to use (will be converted to Symbol)
|
|
87
89
|
# @return [Symbol] The newly set white point
|
|
88
90
|
def white_point=(point)
|
|
@@ -90,7 +92,7 @@ module Abachrome
|
|
|
90
92
|
end
|
|
91
93
|
|
|
92
94
|
# Sets the color model for the color space.
|
|
93
|
-
#
|
|
95
|
+
#
|
|
94
96
|
# @param model [String, Symbol] The new color model to set for this color space
|
|
95
97
|
# @return [Symbol] The color model as a symbolized value
|
|
96
98
|
def color_model=(model)
|
|
@@ -98,9 +100,9 @@ module Abachrome
|
|
|
98
100
|
end
|
|
99
101
|
|
|
100
102
|
# Compares this ColorSpace instance with another to check for equality.
|
|
101
|
-
#
|
|
103
|
+
#
|
|
102
104
|
# Two ColorSpace objects are considered equal if they have the same name.
|
|
103
|
-
#
|
|
105
|
+
#
|
|
104
106
|
# @param other [Object] The object to compare against
|
|
105
107
|
# @return [Boolean] true if other is a ColorSpace with the same name, false otherwise
|
|
106
108
|
def ==(other)
|
|
@@ -110,7 +112,7 @@ module Abachrome
|
|
|
110
112
|
end
|
|
111
113
|
|
|
112
114
|
# Checks if two color spaces are equal.
|
|
113
|
-
#
|
|
115
|
+
#
|
|
114
116
|
# @param other [Abachrome::ColorSpace] The color space to compare with
|
|
115
117
|
# @return [Boolean] true if the color spaces are equal, false otherwise
|
|
116
118
|
def eql?(other)
|
|
@@ -118,7 +120,7 @@ module Abachrome
|
|
|
118
120
|
end
|
|
119
121
|
|
|
120
122
|
# Returns a hash value for the color space based on its name.
|
|
121
|
-
#
|
|
123
|
+
#
|
|
122
124
|
# @return [Integer] A hash value computed from the color space name that can be
|
|
123
125
|
# used for equality comparison and as a hash key.
|
|
124
126
|
def hash
|
|
@@ -180,6 +182,18 @@ module Abachrome
|
|
|
180
182
|
s.white_point = :D65
|
|
181
183
|
s.color_model = :lms
|
|
182
184
|
end
|
|
185
|
+
|
|
186
|
+
ColorSpace.register(:yiq) do |s|
|
|
187
|
+
s.coordinates = %i[y i q]
|
|
188
|
+
s.white_point = :D65
|
|
189
|
+
s.color_model = :yiq
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
ColorSpace.register(:cmyk) do |s|
|
|
193
|
+
s.coordinates = %i[cyan magenta yellow key]
|
|
194
|
+
s.white_point = :D50
|
|
195
|
+
s.color_model = :cmyk
|
|
196
|
+
end
|
|
183
197
|
end
|
|
184
198
|
|
|
185
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
199
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
data/lib/abachrome/converter.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::Converter - Color space conversion registry and orchestration system
|
|
2
4
|
#
|
|
3
5
|
# This module provides the central registry and conversion orchestration for transforming colors
|
|
@@ -22,18 +24,18 @@ module Abachrome
|
|
|
22
24
|
class Converter
|
|
23
25
|
class << self
|
|
24
26
|
# Returns the registry hash used to store color space converters.
|
|
25
|
-
#
|
|
27
|
+
#
|
|
26
28
|
# This method lazily initializes and returns the hash used internally to store
|
|
27
29
|
# converter mappings between different color spaces. This registry is a central
|
|
28
30
|
# repository that maps color space pairs to their appropriate conversion functions.
|
|
29
|
-
#
|
|
31
|
+
#
|
|
30
32
|
# @return [Hash] The converter registry hash, mapping color space pairs to converter functions
|
|
31
33
|
def registry
|
|
32
34
|
@registry ||= {}
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
# Register a converter class for transforming colors between two specific color spaces.
|
|
36
|
-
#
|
|
38
|
+
#
|
|
37
39
|
# @param from_space [Symbol, String] The source color space identifier
|
|
38
40
|
# @param to_space [Symbol, String] The destination color space identifier
|
|
39
41
|
# @param converter_class [Class] The converter class that implements the transformation
|
|
@@ -43,7 +45,7 @@ module Abachrome
|
|
|
43
45
|
end
|
|
44
46
|
|
|
45
47
|
# Converts a color from its current color space to the specified target color space.
|
|
46
|
-
#
|
|
48
|
+
#
|
|
47
49
|
# @param color [Abachrome::Color] The color to convert
|
|
48
50
|
# @param to_space_name [String, Symbol] The name of the target color space
|
|
49
51
|
# @return [Abachrome::Color] The color converted to the target color space
|
|
@@ -65,13 +67,13 @@ module Abachrome
|
|
|
65
67
|
# @example
|
|
66
68
|
# converter = Abachrome::Converter.new
|
|
67
69
|
# converter.register_all_converters
|
|
68
|
-
#
|
|
70
|
+
#
|
|
69
71
|
# Automatically registers all converter classes found in the Converters namespace.
|
|
70
72
|
# The method iterates through constants in the Converters module, identifies classes
|
|
71
73
|
# with naming pattern "FromSpaceToSpace" (e.g., LrgbToOklab), extracts the source
|
|
72
74
|
# and destination color spaces from the class name, and registers the converter
|
|
73
75
|
# class for the corresponding color space conversion.
|
|
74
|
-
#
|
|
76
|
+
#
|
|
75
77
|
# @return [void]
|
|
76
78
|
def register_all_converters
|
|
77
79
|
Converters.constants.each do |const_name|
|
|
@@ -92,7 +94,7 @@ module Abachrome
|
|
|
92
94
|
private
|
|
93
95
|
|
|
94
96
|
# Retrieves a converter function between two color spaces from the registry.
|
|
95
|
-
#
|
|
97
|
+
#
|
|
96
98
|
# @param from_space_name [String, Symbol] The source color space name
|
|
97
99
|
# @param to_space_name [String, Symbol] The target color space name
|
|
98
100
|
# @return [Proc, nil] The conversion function if registered, or nil if no converter exists for the specified color spaces
|
|
@@ -112,4 +114,4 @@ module Abachrome
|
|
|
112
114
|
Converter.register_all_converters
|
|
113
115
|
end
|
|
114
116
|
|
|
115
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
117
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::Converters::Base - Abstract base class for color space converters
|
|
2
4
|
#
|
|
3
5
|
# This class provides the foundation for implementing color space conversion functionality
|
|
@@ -22,7 +24,7 @@ module Abachrome
|
|
|
22
24
|
attr_reader :from_space, :to_space
|
|
23
25
|
|
|
24
26
|
# Initialize a new converter between two color spaces.
|
|
25
|
-
#
|
|
27
|
+
#
|
|
26
28
|
# @param from_space [Abachrome::ColorSpace] The source color space to convert from
|
|
27
29
|
# @param to_space [Abachrome::ColorSpace] The target color space to convert to
|
|
28
30
|
# @return [Abachrome::Converters::Base] A new converter instance
|
|
@@ -32,7 +34,7 @@ module Abachrome
|
|
|
32
34
|
end
|
|
33
35
|
|
|
34
36
|
# Converts a color from one color space to another.
|
|
35
|
-
#
|
|
37
|
+
#
|
|
36
38
|
# @abstract This is an abstract method that must be implemented by subclasses.
|
|
37
39
|
# @param color [Abachrome::Color] The color to convert
|
|
38
40
|
# @return [Abachrome::Color] The converted color
|
|
@@ -42,7 +44,7 @@ module Abachrome
|
|
|
42
44
|
end
|
|
43
45
|
|
|
44
46
|
# Validates that a color uses the expected color model.
|
|
45
|
-
#
|
|
47
|
+
#
|
|
46
48
|
# @param color [Abachrome::Color] The color object to check
|
|
47
49
|
# @param model [Symbol, String] The expected color model
|
|
48
50
|
# @raise [RuntimeError] If the color's model doesn't match the expected model
|
|
@@ -54,10 +56,10 @@ module Abachrome
|
|
|
54
56
|
end
|
|
55
57
|
|
|
56
58
|
# Determines if the converter can handle the given color.
|
|
57
|
-
#
|
|
59
|
+
#
|
|
58
60
|
# This method checks if the color's current color space matches
|
|
59
61
|
# the converter's source color space.
|
|
60
|
-
#
|
|
62
|
+
#
|
|
61
63
|
# @param color [Abachrome::Color] The color to check
|
|
62
64
|
# @return [Boolean] true if the converter can convert from the color's current color space,
|
|
63
65
|
# false otherwise
|
|
@@ -66,7 +68,7 @@ module Abachrome
|
|
|
66
68
|
end
|
|
67
69
|
|
|
68
70
|
# Register a converter class for transforming colors between two specific color spaces.
|
|
69
|
-
#
|
|
71
|
+
#
|
|
70
72
|
# @param from_space_id [Symbol] The identifier of the source color space
|
|
71
73
|
# @param to_space_id [Symbol] The identifier of the destination color space
|
|
72
74
|
# @param converter_class [Class] The converter class that handles the transformation
|
|
@@ -77,7 +79,7 @@ module Abachrome
|
|
|
77
79
|
end
|
|
78
80
|
|
|
79
81
|
# Find a converter for converting between color spaces.
|
|
80
|
-
#
|
|
82
|
+
#
|
|
81
83
|
# @param from_space_id [Symbol, String] The identifier of the source color space
|
|
82
84
|
# @param to_space_id [Symbol, String] The identifier of the destination color space
|
|
83
85
|
# @return [Converter, nil] The converter instance for the specified color spaces, or nil if no converter is found
|
|
@@ -87,10 +89,10 @@ module Abachrome
|
|
|
87
89
|
end
|
|
88
90
|
|
|
89
91
|
# Converts a color from its current color space to a target color space.
|
|
90
|
-
#
|
|
92
|
+
#
|
|
91
93
|
# This method finds the appropriate converter class for the given source and
|
|
92
94
|
# target color spaces and performs the conversion.
|
|
93
|
-
#
|
|
95
|
+
#
|
|
94
96
|
# @param color [Abachrome::Color] The color to convert
|
|
95
97
|
# @param to_space [Abachrome::ColorSpace] The target color space to convert to
|
|
96
98
|
# @return [Abachrome::Color] The converted color in the target color space
|
|
@@ -110,7 +112,7 @@ module Abachrome
|
|
|
110
112
|
|
|
111
113
|
# Validates if a color can be converted from its current color space.
|
|
112
114
|
# Raises an ArgumentError if the color's space doesn't match the expected source space.
|
|
113
|
-
#
|
|
115
|
+
#
|
|
114
116
|
# @param color [Abachrome::Color] The color object to validate
|
|
115
117
|
# @raise [ArgumentError] If the color cannot be converted from its current color space
|
|
116
118
|
# @return [nil] Returns nil if the color is valid for conversion
|
|
@@ -123,4 +125,4 @@ module Abachrome
|
|
|
123
125
|
end
|
|
124
126
|
end
|
|
125
127
|
|
|
126
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
128
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Abachrome::Converters::CmykToSrgb - CMYK to sRGB color space converter
|
|
4
|
+
#
|
|
5
|
+
# This converter transforms colors from the CMYK (Cyan, Magenta, Yellow, Key/Black)
|
|
6
|
+
# color space back to the standard RGB (sRGB) color space for display on screens.
|
|
7
|
+
#
|
|
8
|
+
# Key features:
|
|
9
|
+
# - Implements the standard CMYK to RGB conversion algorithm
|
|
10
|
+
# - Converts from subtractive (ink) to additive (light) color model
|
|
11
|
+
# - Maintains alpha channel transparency values during conversion
|
|
12
|
+
# - Uses BigDecimal arithmetic for precise color science calculations
|
|
13
|
+
# - Handles colors created with UCR/GCR correctly
|
|
14
|
+
#
|
|
15
|
+
# The conversion recombines the ink components (CMYK) back into light components (RGB)
|
|
16
|
+
# using the standard inverse transformation. Note that some CMYK colors may be
|
|
17
|
+
# out of gamut for sRGB displays.
|
|
18
|
+
|
|
19
|
+
module Abachrome
|
|
20
|
+
module Converters
|
|
21
|
+
class CmykToSrgb
|
|
22
|
+
# Converts a color from CMYK color space to sRGB color space.
|
|
23
|
+
# This method applies the standard CMYK to RGB transformation.
|
|
24
|
+
#
|
|
25
|
+
# @param cmyk_color [Abachrome::Color] A color object in the CMYK color space
|
|
26
|
+
# @return [Abachrome::Color] A new color object in the sRGB color space
|
|
27
|
+
# with the same alpha value as the input color
|
|
28
|
+
def self.convert(cmyk_color)
|
|
29
|
+
c, m, y, k = cmyk_color.coordinates.map { |component| AbcDecimal(component) }
|
|
30
|
+
|
|
31
|
+
# Use the CMYK color model's conversion method
|
|
32
|
+
r, g, b = ColorModels::CMYK.to_rgb(c, m, y, k)
|
|
33
|
+
|
|
34
|
+
Color.new(
|
|
35
|
+
ColorSpace.find(:srgb),
|
|
36
|
+
[r, g, b],
|
|
37
|
+
cmyk_color.alpha
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Abachrome
|
|
2
4
|
module Converters
|
|
3
5
|
class LmsToLrgb < Abachrome::Converters::Base
|
|
4
6
|
# Converts a color from LMS color space to linear RGB color space.
|
|
5
|
-
#
|
|
7
|
+
#
|
|
6
8
|
# This method implements the final part of the OKLAB to linear RGB transformation,
|
|
7
9
|
# converting LMS (Long, Medium, Short) coordinates to linear RGB coordinates
|
|
8
10
|
# using the standard transformation matrix. The LMS color space represents
|
|
9
11
|
# the response of the three types of cone cells in the human eye.
|
|
10
|
-
#
|
|
12
|
+
#
|
|
11
13
|
# @param lms_color [Abachrome::Color] The color in LMS color space
|
|
12
14
|
# @raise [ArgumentError] If the input color is not in LMS color space
|
|
13
15
|
# @return [Abachrome::Color] The resulting color in linear RGB color space with
|
|
@@ -35,4 +37,4 @@ module Abachrome
|
|
|
35
37
|
end
|
|
36
38
|
end
|
|
37
39
|
|
|
38
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
40
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Abachrome
|
|
2
4
|
module Converters
|
|
3
5
|
class LmsToSrgb < Abachrome::Converters::Base
|
|
4
6
|
# Converts a color from LMS color space to sRGB color space.
|
|
5
|
-
#
|
|
7
|
+
#
|
|
6
8
|
# This method implements a two-step conversion process:
|
|
7
9
|
# 1. First converts from LMS to linear RGB using the standard transformation matrix
|
|
8
10
|
# 2. Then converts from linear RGB to sRGB by applying gamma correction
|
|
9
|
-
#
|
|
11
|
+
#
|
|
10
12
|
# @param lms_color [Abachrome::Color] The color in LMS color space
|
|
11
13
|
# @raise [ArgumentError] If the input color is not in LMS color space
|
|
12
14
|
# @return [Abachrome::Color] The resulting color in sRGB color space with
|
|
@@ -14,7 +16,7 @@ module Abachrome
|
|
|
14
16
|
def self.convert(lms_color)
|
|
15
17
|
# First convert LMS to linear RGB
|
|
16
18
|
lrgb_color = LmsToLrgb.convert(lms_color)
|
|
17
|
-
|
|
19
|
+
|
|
18
20
|
# Then convert linear RGB to sRGB
|
|
19
21
|
LrgbToSrgb.convert(lrgb_color)
|
|
20
22
|
end
|
|
@@ -22,4 +24,4 @@ module Abachrome
|
|
|
22
24
|
end
|
|
23
25
|
end
|
|
24
26
|
|
|
25
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
27
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Abachrome
|
|
2
4
|
module Converters
|
|
3
5
|
class LmsToXyz < Abachrome::Converters::Base
|
|
4
6
|
# Converts a color from LMS color space to XYZ color space.
|
|
5
|
-
#
|
|
7
|
+
#
|
|
6
8
|
# This method implements the LMS to XYZ transformation using the standard
|
|
7
9
|
# transformation matrix. The LMS color space represents the response of
|
|
8
10
|
# the three types of cone cells in the human eye (Long, Medium, Short),
|
|
9
11
|
# while XYZ is the CIE 1931 color space that forms the basis for most
|
|
10
12
|
# other color space definitions.
|
|
11
|
-
#
|
|
13
|
+
#
|
|
12
14
|
# @param lms_color [Abachrome::Color] The color in LMS color space
|
|
13
15
|
# @raise [ArgumentError] If the input color is not in LMS color space
|
|
14
16
|
# @return [Abachrome::Color] The resulting color in XYZ color space with
|
|
@@ -29,4 +31,4 @@ module Abachrome
|
|
|
29
31
|
end
|
|
30
32
|
end
|
|
31
33
|
|
|
32
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
34
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::Converters::LrgbToOklab - Linear RGB to OKLAB color space converter
|
|
2
4
|
#
|
|
3
5
|
# This converter transforms colors from the linear RGB (LRGB) color space to the OKLAB color space
|
|
@@ -21,11 +23,11 @@ module Abachrome
|
|
|
21
23
|
module Converters
|
|
22
24
|
class LrgbToOklab < Abachrome::Converters::Base
|
|
23
25
|
# Converts a color from linear RGB (LRGB) color space to OKLAB color space.
|
|
24
|
-
#
|
|
26
|
+
#
|
|
25
27
|
# This conversion applies a matrix transformation to the linear RGB values,
|
|
26
28
|
# followed by a non-linear transformation, then another matrix transformation
|
|
27
29
|
# to produce OKLAB coordinates.
|
|
28
|
-
#
|
|
30
|
+
#
|
|
29
31
|
# @param rgb_color [Abachrome::Color] A color in linear RGB (LRGB) color space
|
|
30
32
|
# @raise [ArgumentError] If the provided color is not in LRGB color space
|
|
31
33
|
# @return [Abachrome::Color] The converted color in OKLAB color space with the same alpha value as the input
|
|
@@ -52,4 +54,4 @@ module Abachrome
|
|
|
52
54
|
end
|
|
53
55
|
end
|
|
54
56
|
|
|
55
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
57
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Abachrome::Converters::LrgbToSrgb - Linear RGB to sRGB color space converter
|
|
2
4
|
#
|
|
3
5
|
# This converter transforms colors from the linear RGB (LRGB) color space to the standard RGB (sRGB) color space by applying gamma correction. The conversion process applies the sRGB transfer function which uses different formulas for small and large values to match the non-linear response characteristics of typical display devices.
|
|
@@ -16,7 +18,7 @@ module Abachrome
|
|
|
16
18
|
module Converters
|
|
17
19
|
class LrgbToSrgb < Abachrome::Converters::Base
|
|
18
20
|
# Converts a color from linear RGB to sRGB color space.
|
|
19
|
-
#
|
|
21
|
+
#
|
|
20
22
|
# @param lrgb_color [Abachrome::Color] The color in linear RGB color space to convert
|
|
21
23
|
# @return [Abachrome::Color] A new Color object in sRGB color space with the converted coordinates
|
|
22
24
|
# @raise [TypeError] If the provided color is not in linear RGB color space
|
|
@@ -34,11 +36,11 @@ module Abachrome
|
|
|
34
36
|
end
|
|
35
37
|
|
|
36
38
|
# Converts a linear RGB value to standard RGB color space (sRGB) value.
|
|
37
|
-
#
|
|
39
|
+
#
|
|
38
40
|
# This method implements the standard linearization function used in the sRGB color space.
|
|
39
41
|
# For small values (≤ 0.0031308), a simple linear transformation is applied.
|
|
40
42
|
# For larger values, a power function with gamma correction is used.
|
|
41
|
-
#
|
|
43
|
+
#
|
|
42
44
|
# @param v [AbcDecimal] The linear RGB value to convert
|
|
43
45
|
# @return [AbcDecimal] The corresponding sRGB value, preserving the sign of the input
|
|
44
46
|
def self.to_srgb(v)
|
|
@@ -54,4 +56,4 @@ module Abachrome
|
|
|
54
56
|
end
|
|
55
57
|
end
|
|
56
58
|
|
|
57
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
59
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Abachrome
|
|
2
4
|
module Converters
|
|
3
5
|
class LrgbToXyz < Abachrome::Converters::Base
|
|
4
6
|
# Converts a color from linear RGB color space to XYZ color space.
|
|
5
|
-
#
|
|
7
|
+
#
|
|
6
8
|
# This method implements the linear RGB to XYZ transformation using the standard
|
|
7
9
|
# transformation matrix for the sRGB color space with D65 white point. The XYZ
|
|
8
10
|
# color space is the CIE 1931 color space that forms the basis for most other
|
|
9
11
|
# color space definitions and serves as a device-independent reference.
|
|
10
|
-
#
|
|
12
|
+
#
|
|
11
13
|
# @param lrgb_color [Abachrome::Color] The color in linear RGB color space
|
|
12
14
|
# @raise [ArgumentError] If the input color is not in linear RGB color space
|
|
13
15
|
# @return [Abachrome::Color] The resulting color in XYZ color space with
|
|
@@ -28,4 +30,4 @@ module Abachrome
|
|
|
28
30
|
end
|
|
29
31
|
end
|
|
30
32
|
|
|
31
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
33
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Abachrome
|
|
2
4
|
module Converters
|
|
3
5
|
class OklabToLms < Abachrome::Converters::Base
|
|
4
6
|
# Converts a color from OKLAB color space to LMS color space.
|
|
5
|
-
#
|
|
7
|
+
#
|
|
6
8
|
# This method implements the first part of the OKLAB to linear RGB transformation,
|
|
7
9
|
# converting OKLAB coordinates to the intermediate LMS (Long, Medium, Short) color space
|
|
8
10
|
# which represents the response of the three types of cone cells in the human eye.
|
|
9
|
-
#
|
|
11
|
+
#
|
|
10
12
|
# @param oklab_color [Abachrome::Color] The color in OKLAB color space
|
|
11
13
|
# @raise [ArgumentError] If the input color is not in OKLAB color space
|
|
12
14
|
# @return [Abachrome::Color] The resulting color in LMS color space with
|
|
@@ -16,15 +18,15 @@ module Abachrome
|
|
|
16
18
|
|
|
17
19
|
l, a, b = oklab_color.coordinates.map { |_| AbcDecimal(_) }
|
|
18
20
|
|
|
19
|
-
l_ = AbcDecimal(
|
|
21
|
+
l_ = AbcDecimal(l +
|
|
20
22
|
(AD("0.39633779217376785678") * a) +
|
|
21
23
|
(AD("0.21580375806075880339") * b))
|
|
22
24
|
|
|
23
|
-
m_ = AbcDecimal(
|
|
25
|
+
m_ = AbcDecimal(l -
|
|
24
26
|
(a * AD("-0.1055613423236563494")) +
|
|
25
27
|
(b * AD("-0.063854174771705903402")))
|
|
26
|
-
|
|
27
|
-
s_ = AbcDecimal(
|
|
28
|
+
|
|
29
|
+
s_ = AbcDecimal(l -
|
|
28
30
|
(a * AD("-0.089484182094965759684")) +
|
|
29
31
|
(b * AD("-1.2914855378640917399")))
|
|
30
32
|
|
|
@@ -39,4 +41,4 @@ module Abachrome
|
|
|
39
41
|
end
|
|
40
42
|
end
|
|
41
43
|
|
|
42
|
-
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
44
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|