abachrome 0.1.0 → 0.1.1

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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.envrc +3 -0
  3. data/README.md +10 -0
  4. data/Rakefile +12 -0
  5. data/devenv.lock +88 -17
  6. data/devenv.nix +2 -1
  7. data/devenv.yaml +5 -12
  8. data/lib/abachrome/abc_decimal.rb +202 -8
  9. data/lib/abachrome/color.rb +77 -2
  10. data/lib/abachrome/color_mixins/blend.rb +53 -2
  11. data/lib/abachrome/color_mixins/lighten.rb +49 -2
  12. data/lib/abachrome/color_mixins/to_colorspace.rb +67 -2
  13. data/lib/abachrome/color_mixins/to_lrgb.rb +70 -2
  14. data/lib/abachrome/color_mixins/to_oklab.rb +67 -2
  15. data/lib/abachrome/color_mixins/to_oklch.rb +60 -2
  16. data/lib/abachrome/color_mixins/to_srgb.rb +77 -2
  17. data/lib/abachrome/color_models/hsv.rb +25 -2
  18. data/lib/abachrome/color_models/oklab.rb +19 -2
  19. data/lib/abachrome/color_models/oklch.rb +42 -2
  20. data/lib/abachrome/color_models/rgb.rb +28 -2
  21. data/lib/abachrome/color_space.rb +76 -2
  22. data/lib/abachrome/converter.rb +57 -3
  23. data/lib/abachrome/converters/base.rb +69 -2
  24. data/lib/abachrome/converters/lrgb_to_oklab.rb +28 -2
  25. data/lib/abachrome/converters/lrgb_to_srgb.rb +27 -2
  26. data/lib/abachrome/converters/oklab_to_lrgb.rb +34 -2
  27. data/lib/abachrome/converters/oklab_to_oklch.rb +31 -2
  28. data/lib/abachrome/converters/oklab_to_srgb.rb +27 -2
  29. data/lib/abachrome/converters/oklch_to_lrgb.rb +25 -2
  30. data/lib/abachrome/converters/oklch_to_oklab.rb +27 -2
  31. data/lib/abachrome/converters/oklch_to_srgb.rb +26 -2
  32. data/lib/abachrome/converters/srgb_to_lrgb.rb +26 -2
  33. data/lib/abachrome/converters/srgb_to_oklab.rb +28 -2
  34. data/lib/abachrome/converters/srgb_to_oklch.rb +27 -2
  35. data/lib/abachrome/gamut/base.rb +2 -2
  36. data/lib/abachrome/gamut/p3.rb +2 -2
  37. data/lib/abachrome/gamut/rec2020.rb +2 -2
  38. data/lib/abachrome/gamut/srgb.rb +20 -2
  39. data/lib/abachrome/illuminants/base.rb +2 -2
  40. data/lib/abachrome/illuminants/d50.rb +2 -2
  41. data/lib/abachrome/illuminants/d55.rb +2 -2
  42. data/lib/abachrome/illuminants/d65.rb +2 -2
  43. data/lib/abachrome/illuminants/d75.rb +2 -2
  44. data/lib/abachrome/named/css.rb +149 -158
  45. data/lib/abachrome/outputs/css.rb +2 -2
  46. data/lib/abachrome/palette.rb +112 -2
  47. data/lib/abachrome/palette_mixins/interpolate.rb +20 -2
  48. data/lib/abachrome/palette_mixins/resample.rb +2 -2
  49. data/lib/abachrome/palette_mixins/stretch_luminance.rb +2 -2
  50. data/lib/abachrome/parsers/hex.rb +2 -2
  51. data/lib/abachrome/to_abcd.rb +10 -2
  52. data/lib/abachrome/version.rb +2 -2
  53. data/lib/abachrome.rb +78 -2
  54. data/logo.png +0 -0
  55. data/logo.webp +0 -0
  56. metadata +15 -67
@@ -1,39 +1,100 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::Base - Abstract base class for color space converters
2
+ #
3
+ # This class provides the foundation for implementing color space conversion functionality
4
+ # within the Abachrome library. It defines the interface that all converter classes must
5
+ # implement and provides common validation and utility methods for color transformations.
6
+ #
7
+ # Key features:
8
+ # - Abstract conversion interface requiring subclasses to implement #convert method
9
+ # - Color model validation to ensure proper conversion compatibility
10
+ # - Converter registration and lookup system for managing conversion mappings
11
+ # - Source and target color space compatibility checking
12
+ # - Base functionality for building specific converter implementations
13
+ #
14
+ # All converter classes in the Abachrome system inherit from this base class and implement
15
+ # the specific mathematical transformations needed to convert colors between different
16
+ # color spaces such as sRGB, OKLAB, OKLCH, and linear RGB. The class follows a naming
17
+ # convention pattern (FromSpaceToSpace) for automatic registration and discovery.
2
18
 
3
19
  module Abachrome
4
20
  module Converters
5
21
  class Base
6
22
  attr_reader :from_space, :to_space
7
23
 
24
+ # Initialize a new converter between two color spaces.
25
+ #
26
+ # @param from_space [Abachrome::ColorSpace] The source color space to convert from
27
+ # @param to_space [Abachrome::ColorSpace] The target color space to convert to
28
+ # @return [Abachrome::Converters::Base] A new converter instance
8
29
  def initialize(from_space, to_space)
9
30
  @from_space = from_space
10
31
  @to_space = to_space
11
32
  end
12
33
 
34
+ # Converts a color from one color space to another.
35
+ #
36
+ # @abstract This is an abstract method that must be implemented by subclasses.
37
+ # @param color [Abachrome::Color] The color to convert
38
+ # @return [Abachrome::Color] The converted color
39
+ # @raise [NotImplementedError] If the subclass doesn't implement this method
13
40
  def convert(color)
14
41
  raise NotImplementedError, "Subclasses must implement #convert"
15
42
  end
16
43
 
44
+ # Validates that a color uses the expected color model.
45
+ #
46
+ # @param color [Abachrome::Color] The color object to check
47
+ # @param model [Symbol, String] The expected color model
48
+ # @raise [RuntimeError] If the color's model doesn't match the expected model
49
+ # @return [nil] If the color's model matches the expected model
17
50
  def self.raise_unless(color, model)
18
51
  return if color.color_space.color_model == model
19
52
 
20
53
  raise "#{color} is #{color.color_space.color_model}), expecting #{model}"
21
54
  end
22
55
 
56
+ # Determines if the converter can handle the given color.
57
+ #
58
+ # This method checks if the color's current color space matches
59
+ # the converter's source color space.
60
+ #
61
+ # @param color [Abachrome::Color] The color to check
62
+ # @return [Boolean] true if the converter can convert from the color's current color space,
63
+ # false otherwise
23
64
  def can_convert?(color)
24
65
  color.color_space == from_space
25
66
  end
26
67
 
68
+ # Register a converter class for transforming colors between two specific color spaces.
69
+ #
70
+ # @param from_space_id [Symbol] The identifier of the source color space
71
+ # @param to_space_id [Symbol] The identifier of the destination color space
72
+ # @param converter_class [Class] The converter class that handles the transformation
73
+ # @return [void]
27
74
  def self.register(from_space_id, to_space_id, converter_class)
28
75
  @converters ||= {}
29
76
  @converters[[from_space_id, to_space_id]] = converter_class
30
77
  end
31
78
 
79
+ # Find a converter for converting between color spaces.
80
+ #
81
+ # @param from_space_id [Symbol, String] The identifier of the source color space
82
+ # @param to_space_id [Symbol, String] The identifier of the destination color space
83
+ # @return [Converter, nil] The converter instance for the specified color spaces, or nil if no converter is found
32
84
  def self.find_converter(from_space_id, to_space_id)
33
85
  @converters ||= {}
34
86
  @converters[[from_space_id, to_space_id]]
35
87
  end
36
88
 
89
+ # Converts a color from its current color space to a target color space.
90
+ #
91
+ # This method finds the appropriate converter class for the given source and
92
+ # target color spaces and performs the conversion.
93
+ #
94
+ # @param color [Abachrome::Color] The color to convert
95
+ # @param to_space [Abachrome::ColorSpace] The target color space to convert to
96
+ # @return [Abachrome::Color] The converted color in the target color space
97
+ # @raise [ConversionError] If no converter is found for the given color spaces
37
98
  def self.convert(color, to_space)
38
99
  converter_class = find_converter(color.color_space.id, to_space.id)
39
100
  unless converter_class
@@ -47,6 +108,12 @@ module Abachrome
47
108
 
48
109
  private
49
110
 
111
+ # Validates if a color can be converted from its current color space.
112
+ # Raises an ArgumentError if the color's space doesn't match the expected source space.
113
+ #
114
+ # @param color [Abachrome::Color] The color object to validate
115
+ # @raise [ArgumentError] If the color cannot be converted from its current color space
116
+ # @return [nil] Returns nil if the color is valid for conversion
50
117
  def validate_color!(color)
51
118
  return if can_convert?(color)
52
119
 
@@ -54,4 +121,4 @@ module Abachrome
54
121
  end
55
122
  end
56
123
  end
57
- end
124
+ end
@@ -1,8 +1,34 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::LrgbToOklab - Linear RGB to OKLAB color space converter
2
+ #
3
+ # This converter transforms colors from the linear RGB (LRGB) color space to the OKLAB color space
4
+ # using the standard OKLAB transformation matrices. The conversion process applies a series of
5
+ # matrix transformations and non-linear operations to accurately map linear RGB coordinates to
6
+ # the perceptually uniform OKLAB color space.
7
+ #
8
+ # Key features:
9
+ # - Implements the official OKLAB transformation algorithm with high-precision matrices
10
+ # - Converts linear RGB values through intermediate LMS color space representation
11
+ # - Applies cube root transformation for perceptual uniformity in the OKLAB space
12
+ # - Maintains alpha channel transparency values during conversion
13
+ # - Uses AbcDecimal arithmetic for precise color science calculations
14
+ # - Validates input color space to ensure proper linear RGB source data
15
+ #
16
+ # The OKLAB color space provides better perceptual uniformity compared to traditional RGB spaces,
17
+ # making it ideal for color manipulation operations like blending, lightness adjustments, and
18
+ # gamut mapping where human visual perception accuracy is important.
2
19
 
3
20
  module Abachrome
4
21
  module Converters
5
22
  class LrgbToOklab < Abachrome::Converters::Base
23
+ # Converts a color from linear RGB (LRGB) color space to OKLAB color space.
24
+ #
25
+ # This conversion applies a matrix transformation to the linear RGB values,
26
+ # followed by a non-linear transformation, then another matrix transformation
27
+ # to produce OKLAB coordinates.
28
+ #
29
+ # @param rgb_color [Abachrome::Color] A color in linear RGB (LRGB) color space
30
+ # @raise [ArgumentError] If the provided color is not in LRGB color space
31
+ # @return [Abachrome::Color] The converted color in OKLAB color space with the same alpha value as the input
6
32
  def self.convert(rgb_color)
7
33
  raise_unless rgb_color, :lrgb
8
34
 
@@ -24,4 +50,4 @@ module Abachrome
24
50
  end
25
51
  end
26
52
  end
27
- end
53
+ end
@@ -1,8 +1,25 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::LrgbToSrgb - Linear RGB to sRGB color space converter
2
+ #
3
+ # 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.
4
+ #
5
+ # Key features:
6
+ # - Implements the standard sRGB gamma correction algorithm with precise threshold handling
7
+ # - Converts linear RGB values to gamma-corrected sRGB values for proper display representation
8
+ # - Applies different transformation functions based on value magnitude (linear vs power function)
9
+ # - Maintains alpha channel transparency values during conversion
10
+ # - Uses AbcDecimal arithmetic for precise color science calculations
11
+ # - Validates input color space to ensure proper linear RGB source data
12
+ #
13
+ # The sRGB color space is the standard RGB color space for web content and most consumer displays, providing gamma correction that better matches human visual perception and display device characteristics compared to linear RGB values.
2
14
 
3
15
  module Abachrome
4
16
  module Converters
5
17
  class LrgbToSrgb < Abachrome::Converters::Base
18
+ # Converts a color from linear RGB to sRGB color space.
19
+ #
20
+ # @param lrgb_color [Abachrome::Color] The color in linear RGB color space to convert
21
+ # @return [Abachrome::Color] A new Color object in sRGB color space with the converted coordinates
22
+ # @raise [TypeError] If the provided color is not in linear RGB color space
6
23
  def self.convert(lrgb_color)
7
24
  raise_unless lrgb_color, :lrgb
8
25
  r, g, b = lrgb_color.coordinates.map { |c| to_srgb(AbcDecimal(c)) }
@@ -16,6 +33,14 @@ module Abachrome
16
33
  )
17
34
  end
18
35
 
36
+ # Converts a linear RGB value to standard RGB color space (sRGB) value.
37
+ #
38
+ # This method implements the standard linearization function used in the sRGB color space.
39
+ # For small values (≤ 0.0031308), a simple linear transformation is applied.
40
+ # For larger values, a power function with gamma correction is used.
41
+ #
42
+ # @param v [AbcDecimal] The linear RGB value to convert
43
+ # @return [AbcDecimal] The corresponding sRGB value, preserving the sign of the input
19
44
  def self.to_srgb(v)
20
45
  v_abs = v.abs
21
46
  v_sign = v.negative? ? -1 : 1
@@ -27,4 +52,4 @@ module Abachrome
27
52
  end
28
53
  end
29
54
  end
30
- end
55
+ end
@@ -1,8 +1,40 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::OklabToLrgb - OKLAB to Linear RGB color space converter
2
+ #
3
+ # This converter transforms colors from the OKLAB color space to the linear RGB (LRGB) color space
4
+ # using the standard OKLAB inverse transformation matrices. The conversion process applies a series of
5
+ # matrix transformations and cubic operations to accurately map OKLAB coordinates back to the
6
+ # linear RGB color space, which represents colors with a linear relationship to light intensity.
7
+ #
8
+ # Key features:
9
+ # - Implements the official OKLAB to linear RGB inverse transformation algorithm with high-precision matrices
10
+ # - Converts OKLAB values through intermediate L'M'S' and LMS color space representations
11
+ # - Applies cubic operations to reverse the perceptual uniformity transformations
12
+ # - Clamps negative RGB values to zero to ensure valid linear RGB output
13
+ # - Maintains alpha channel transparency values during conversion
14
+ # - Uses AbcDecimal arithmetic for precise color science calculations
15
+ # - Validates input color space to ensure proper OKLAB source data
16
+ #
17
+ # The linear RGB color space provides the foundation for further conversions to display-ready
18
+ # color spaces like sRGB, making this converter essential for the color transformation pipeline
19
+ # when working with perceptually uniform OKLAB color manipulations.
2
20
 
3
21
  module Abachrome
4
22
  module Converters
5
23
  class OklabToLrgb < Abachrome::Converters::Base
24
+ # Converts a color from OKLAB color space to linear RGB color space.
25
+ #
26
+ # The method implements the OKLAB to linear RGB transformation matrix based on
27
+ # standard color science algorithms. It first confirms the input is in OKLAB
28
+ # space, then applies the transformation through several steps:
29
+ # 1. Converts OKLAB coordinates to intermediate L'M'S' values
30
+ # 2. Applies a cubic operation to get LMS values
31
+ # 3. Transforms LMS to linear RGB
32
+ # 4. Clamps negative values to zero
33
+ #
34
+ # @param oklab_color [Abachrome::Color] The color in OKLAB color space
35
+ # @raise [ArgumentError] If the input color is not in OKLAB color space
36
+ # @return [Abachrome::Color] The resulting color in linear RGB color space with
37
+ # the same alpha as the input color
6
38
  def self.convert(oklab_color)
7
39
  raise_unless oklab_color, :oklab
8
40
 
@@ -39,4 +71,4 @@ module Abachrome
39
71
  end
40
72
  end
41
73
  end
42
- end
74
+ end
@@ -1,8 +1,37 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::OklabToOklch - OKLAB to OKLCH color space converter
2
+ #
3
+ # This converter transforms colors from the OKLAB color space to the OKLCH color space
4
+ # using cylindrical coordinate conversion. The transformation converts the rectangular
5
+ # coordinates (L, a, b) to cylindrical coordinates (L, C, h) where lightness remains
6
+ # unchanged, chroma is calculated as the Euclidean distance in the a-b plane, and hue
7
+ # is calculated as the angle in the a-b plane expressed in degrees.
8
+ #
9
+ # Key features:
10
+ # - Converts OKLAB rectangular coordinates to OKLCH cylindrical coordinates
11
+ # - Preserves lightness component unchanged during conversion
12
+ # - Calculates chroma as sqrt(a² + b²) for colorfulness representation
13
+ # - Computes hue angle using atan2 function and normalizes to 0-360 degree range
14
+ # - Maintains alpha channel transparency values during conversion
15
+ # - Uses AbcDecimal arithmetic for precise color science calculations
16
+ # - Validates input color space to ensure proper OKLAB source data
17
+ #
18
+ # The OKLCH color space provides an intuitive interface for color manipulation through
19
+ # its cylindrical coordinate system, making it ideal for hue adjustments, saturation
20
+ # modifications, and other color operations that benefit from polar coordinates.
2
21
 
3
22
  module Abachrome
4
23
  module Converters
5
24
  class OklabToOklch < Abachrome::Converters::Base
25
+ # Converts a color from OKLAB color space to OKLCH color space.
26
+ # The method performs a mathematical transformation from the rectangular
27
+ # coordinates (L, a, b) to cylindrical coordinates (L, C, h), where:
28
+ # - L (lightness) remains the same
29
+ # - C (chroma) is calculated as the Euclidean distance from the origin in the a-b plane
30
+ # - h (hue) is calculated as the angle in the a-b plane
31
+ #
32
+ # @param oklab_color [Abachrome::Color] A color in the OKLAB color space
33
+ # @raise [ArgumentError] If the provided color is not in OKLAB color space
34
+ # @return [Abachrome::Color] The equivalent color in OKLCH color space with the same alpha value
6
35
  def self.convert(oklab_color)
7
36
  raise_unless oklab_color, :oklab
8
37
 
@@ -20,4 +49,4 @@ module Abachrome
20
49
  end
21
50
  end
22
51
  end
23
- end
52
+ end
@@ -1,8 +1,33 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::OklabToSrgb - OKLAB to sRGB color space converter
2
+ #
3
+ # This converter transforms colors from the OKLAB color space to the standard RGB (sRGB) color space
4
+ # through a two-step conversion process. The transformation first converts OKLAB coordinates to
5
+ # linear RGB as an intermediate step, then applies gamma correction to produce the final sRGB
6
+ # values suitable for display on standard monitors and web applications.
7
+ #
8
+ # Key features:
9
+ # - Two-stage conversion pipeline: OKLAB → Linear RGB → sRGB
10
+ # - Leverages existing OklabToLrgb and LrgbToSrgb converters for modular transformation
11
+ # - Maintains alpha channel transparency values during conversion
12
+ # - Applies proper gamma correction for display-ready color values
13
+ # - Uses AbcDecimal arithmetic for precise color science calculations
14
+ # - Validates input color space to ensure proper OKLAB source data
15
+ #
16
+ # The sRGB color space is the standard RGB color space for web content and most consumer
17
+ # displays, providing gamma-corrected values that properly represent colors on typical
18
+ # display devices while maintaining compatibility with web standards and digital media formats.
2
19
 
3
20
  module Abachrome
4
21
  module Converters
5
22
  class OklabToSrgb < Abachrome::Converters::Base
23
+ # Converts a color from the Oklab color space to the sRGB color space.
24
+ # This conversion is performed in two steps:
25
+ # 1. First converts from Oklab to linear RGB
26
+ # 2. Then converts from linear RGB to sRGB
27
+ #
28
+ # @param oklab_color [Color] A color in the Oklab color space
29
+ # @raise [ArgumentError] If the provided color is not in the Oklab color space
30
+ # @return [Color] The converted color in the sRGB color space
6
31
  def self.convert(oklab_color)
7
32
  raise_unless oklab_color, :oklab
8
33
 
@@ -14,4 +39,4 @@ module Abachrome
14
39
  end
15
40
  end
16
41
  end
17
- end
42
+ end
@@ -1,4 +1,21 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::OklchToLrgb - OKLCH to Linear RGB color space converter
2
+ #
3
+ # This converter transforms colors from the OKLCH color space to the linear RGB (LRGB) color space
4
+ # through a two-step conversion process. The transformation first converts OKLCH cylindrical
5
+ # coordinates to OKLAB rectangular coordinates, then applies the standard OKLAB to linear RGB
6
+ # transformation matrices to produce the final linear RGB values.
7
+ #
8
+ # Key features:
9
+ # - Two-stage conversion pipeline: OKLCH → OKLAB → Linear RGB
10
+ # - Leverages existing OklchToOklab and OklabToLrgb converters for modular transformation
11
+ # - Converts cylindrical coordinates (lightness, chroma, hue) to linear light intensity values
12
+ # - Maintains alpha channel transparency values during conversion
13
+ # - Uses AbcDecimal arithmetic for precise color science calculations
14
+ # - Validates input color space to ensure proper OKLCH source data
15
+ #
16
+ # The linear RGB color space provides the foundation for further conversions to display-ready
17
+ # color spaces like sRGB, making this converter essential for the color transformation pipeline
18
+ # when working with OKLCH color manipulations that need to be rendered on standard displays.
2
19
 
3
20
  require_relative "oklab_to_lrgb"
4
21
  require_relative "oklch_to_oklab"
@@ -6,10 +23,16 @@ require_relative "oklch_to_oklab"
6
23
  module Abachrome
7
24
  module Converters
8
25
  class OklchToLrgb < Abachrome::Converters::Base
26
+ # Converts a color from OKLCH color space to linear RGB color space.
27
+ # This is a two-step conversion process that first converts from OKLCH to OKLAB,
28
+ # then from OKLAB to linear RGB.
29
+ #
30
+ # @param oklch_color [Abachrome::Color] A color in the OKLCH color space
31
+ # @return [Abachrome::Color] The resulting color in linear RGB color space
9
32
  def self.convert(oklch_color)
10
33
  oklab_color = OklchToOklab.convert(oklch_color)
11
34
  OklabToLrgb.convert(oklab_color)
12
35
  end
13
36
  end
14
37
  end
15
- end
38
+ end
@@ -1,8 +1,33 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::OklchToOklab - OKLCH to OKLAB color space converter
2
+ #
3
+ # This converter transforms colors from the OKLCH color space to the OKLAB color space
4
+ # using cylindrical to rectangular coordinate conversion. The transformation converts the
5
+ # cylindrical coordinates (L, C, h) to rectangular coordinates (L, a, b) where lightness
6
+ # remains unchanged, and the a and b components are calculated from chroma and hue using
7
+ # trigonometric functions (cosine and sine respectively).
8
+ #
9
+ # Key features:
10
+ # - Converts OKLCH cylindrical coordinates to OKLAB rectangular coordinates
11
+ # - Preserves lightness component unchanged during conversion
12
+ # - Calculates a component as chroma × cos(hue) for green-red axis positioning
13
+ # - Calculates b component as chroma × sin(hue) for blue-yellow axis positioning
14
+ # - Converts hue angle from degrees to radians for trigonometric calculations
15
+ # - Maintains alpha channel transparency values during conversion
16
+ # - Uses AbcDecimal arithmetic for precise color science calculations
17
+ # - Validates input color space to ensure proper OKLCH source data
18
+ #
19
+ # The OKLAB color space provides the foundation for further conversions to other color
20
+ # spaces and serves as an intermediate step in the color transformation pipeline when
21
+ # working with OKLCH color manipulations that need to be converted to display-ready formats.
2
22
 
3
23
  module Abachrome
4
24
  module Converters
5
25
  class OklchToOklab < Abachrome::Converters::Base
26
+ # Converts a color from OKLCH color space to OKLAB color space.
27
+ #
28
+ # @param oklch_color [Abachrome::Color] The color in OKLCH format to convert
29
+ # @return [Abachrome::Color] The converted color in OKLAB format
30
+ # @raise [StandardError] If the provided color is not in OKLCH color space
6
31
  def self.convert(oklch_color)
7
32
  raise_unless oklch_color, :oklch
8
33
 
@@ -20,4 +45,4 @@ module Abachrome
20
45
  end
21
46
  end
22
47
  end
23
- end
48
+ end
@@ -1,4 +1,22 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::OklchToSrgb - OKLCH to sRGB color space converter
2
+ #
3
+ # This converter transforms colors from the OKLCH color space to the standard RGB (sRGB) color space
4
+ # through a two-step conversion process. The transformation first converts OKLCH cylindrical
5
+ # coordinates to OKLAB rectangular coordinates, then applies the standard OKLAB to sRGB
6
+ # transformation pipeline to produce the final gamma-corrected sRGB values.
7
+ #
8
+ # Key features:
9
+ # - Two-stage conversion pipeline: OKLCH → OKLAB → sRGB
10
+ # - Leverages existing OklchToOklab and OklabToSrgb converters for modular transformation
11
+ # - Converts cylindrical coordinates (lightness, chroma, hue) to display-ready RGB values
12
+ # - Maintains alpha channel transparency values during conversion
13
+ # - Applies proper gamma correction for display on standard monitors and web applications
14
+ # - Uses AbcDecimal arithmetic for precise color science calculations
15
+ # - Validates input color space to ensure proper OKLCH source data
16
+ #
17
+ # The sRGB color space is the standard RGB color space for web content and most consumer
18
+ # displays, providing gamma-corrected values that properly represent colors on typical
19
+ # display devices while maintaining compatibility with web standards and digital media formats.
2
20
 
3
21
  require_relative "oklch_to_oklab"
4
22
  require_relative "oklab_to_srgb"
@@ -6,6 +24,12 @@ require_relative "oklab_to_srgb"
6
24
  module Abachrome
7
25
  module Converters
8
26
  class OklchToSrgb < Abachrome::Converters::Base
27
+ # Converts a color from OKLCH color space to sRGB color space.
28
+ # This is done by first converting from OKLCH to OKLAB,
29
+ # then from OKLAB to sRGB.
30
+ #
31
+ # @param oklch_color [Abachrome::Color] Color in OKLCH color space
32
+ # @return [Abachrome::Color] The converted color in sRGB color space
9
33
  def self.convert(oklch_color)
10
34
  # Convert OKLCh to OKLab first
11
35
  oklab_color = OklchToOklab.convert(oklch_color)
@@ -15,4 +39,4 @@ module Abachrome
15
39
  end
16
40
  end
17
41
  end
18
- end
42
+ end
@@ -1,8 +1,26 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::SrgbToLrgb - sRGB to Linear RGB color space converter
2
+ #
3
+ # This converter transforms colors from the standard RGB (sRGB) color space to the linear RGB (LRGB) color space by removing gamma correction. The conversion process applies the inverse sRGB transfer function which uses different formulas for small and large values to convert from the gamma-corrected sRGB representation to linear light intensity values.
4
+ #
5
+ # Key features:
6
+ # - Implements the standard sRGB to linear RGB conversion algorithm with precise threshold handling
7
+ # - Converts gamma-corrected sRGB values to linear RGB values for accurate color calculations
8
+ # - Applies different transformation functions based on value magnitude (linear vs power function)
9
+ # - Maintains alpha channel transparency values during conversion
10
+ # - Uses AbcDecimal arithmetic for precise color science calculations
11
+ # - Validates input color space to ensure proper sRGB source data
12
+ #
13
+ # The linear RGB color space provides a linear relationship between stored numeric values and actual light intensity, making it essential for accurate color calculations and serving as an intermediate color space for many color transformations, particularly when converting between different color models.
2
14
 
3
15
  module Abachrome
4
16
  module Converters
5
17
  class SrgbToLrgb
18
+ # Converts a color from sRGB color space to linear RGB color space.
19
+ # This method performs gamma correction by linearizing each sRGB coordinate.
20
+ #
21
+ # @param srgb_color [Abachrome::Color] A color object in the sRGB color space
22
+ # @return [Abachrome::Color] A new color object in the linear RGB (LRGB) color space
23
+ # with the same alpha value as the input color
6
24
  def self.convert(srgb_color)
7
25
  r, g, b = srgb_color.coordinates.map { |c| to_linear(AbcDecimal(c)) }
8
26
 
@@ -13,6 +31,12 @@ module Abachrome
13
31
  )
14
32
  end
15
33
 
34
+ # Converts a sRGB component to its linear RGB equivalent.
35
+ # This conversion applies the appropriate gamma correction to transform an sRGB value
36
+ # into a linear RGB value.
37
+ #
38
+ # @param v [AbcDecimal, Numeric] The sRGB component value to convert (typically in range 0-1)
39
+ # @return [AbcDecimal] The corresponding linear RGB component value
16
40
  def self.to_linear(v)
17
41
  v_abs = v.abs
18
42
  v_sign = v.negative? ? -1 : 1
@@ -24,4 +48,4 @@ module Abachrome
24
48
  end
25
49
  end
26
50
  end
27
- end
51
+ end
@@ -1,8 +1,34 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::SrgbToOklab - sRGB to OKLAB color space converter
2
+ #
3
+ # This converter transforms colors from the standard RGB (sRGB) color space to the OKLAB color space
4
+ # through a two-step conversion process. The transformation first converts sRGB gamma-corrected
5
+ # values to linear RGB as an intermediate step, then applies the standard OKLAB transformation
6
+ # matrices to produce the final perceptually uniform OKLAB coordinates.
7
+ #
8
+ # Key features:
9
+ # - Two-stage conversion pipeline: sRGB → Linear RGB → OKLAB
10
+ # - Leverages existing SrgbToLrgb and LrgbToOklab converters for modular transformation
11
+ # - Removes gamma correction and applies perceptual uniformity transformations
12
+ # - Maintains alpha channel transparency values during conversion
13
+ # - Uses AbcDecimal arithmetic for precise color science calculations
14
+ # - Validates input color space to ensure proper sRGB source data
15
+ #
16
+ # The OKLAB color space provides better perceptual uniformity compared to traditional RGB spaces,
17
+ # making it ideal for color manipulation operations like blending, lightness adjustments, and
18
+ # gamut mapping where human visual perception accuracy is important. This converter enables
19
+ # seamless transformation from display-ready sRGB values to the scientifically accurate OKLAB
20
+ # representation for advanced color processing workflows.
2
21
 
3
22
  module Abachrome
4
23
  module Converters
5
24
  class SrgbToOklab
25
+ # Converts a color from sRGB color space to Oklab color space.
26
+ # The conversion happens in two steps:
27
+ # 1. sRGB is first converted to linear RGB
28
+ # 2. Linear RGB is then converted to Oklab
29
+ #
30
+ # @param srgb_color [Abachrome::Color] The color in sRGB color space to convert
31
+ # @return [Abachrome::Color] The converted color in Oklab color space
6
32
  def self.convert(srgb_color)
7
33
  # First convert sRGB to linear RGB
8
34
  lrgb_color = SrgbToLrgb.convert(srgb_color)
@@ -12,4 +38,4 @@ module Abachrome
12
38
  end
13
39
  end
14
40
  end
15
- end
41
+ end
@@ -1,4 +1,22 @@
1
- # frozen_string_literal: true
1
+ # Abachrome::Converters::SrgbToOklch - sRGB to OKLCH color space converter
2
+ #
3
+ # This converter transforms colors from the standard RGB (sRGB) color space to the OKLCH color space
4
+ # through a two-step conversion process. The transformation first converts sRGB gamma-corrected
5
+ # values to OKLAB rectangular coordinates as an intermediate step, then applies cylindrical coordinate
6
+ # conversion to produce the final OKLCH values with lightness, chroma, and hue components.
7
+ #
8
+ # Key features:
9
+ # - Two-stage conversion pipeline: sRGB → OKLAB → OKLCH
10
+ # - Leverages existing SrgbToOklab and OklabToOklch converters for modular transformation
11
+ # - Converts display-ready RGB values to perceptually uniform cylindrical coordinates
12
+ # - Maintains alpha channel transparency values during conversion
13
+ # - Uses AbcDecimal arithmetic for precise color science calculations
14
+ # - Validates input color space to ensure proper sRGB source data
15
+ #
16
+ # The OKLCH color space provides an intuitive interface for color manipulation through its
17
+ # cylindrical coordinate system, making it ideal for hue adjustments, saturation modifications,
18
+ # and other color operations that benefit from polar coordinates while maintaining perceptual
19
+ # uniformity for natural-looking color transformations.
2
20
 
3
21
  require_relative "srgb_to_oklab"
4
22
  require_relative "oklab_to_oklch"
@@ -6,6 +24,13 @@ require_relative "oklab_to_oklch"
6
24
  module Abachrome
7
25
  module Converters
8
26
  class SrgbToOklch < Abachrome::Converters::Base
27
+ # Converts an sRGB color to OKLCH color space
28
+ #
29
+ # @param srgb_color [Abachrome::Color] The color in sRGB color space to convert
30
+ # @return [Abachrome::Color] The converted color in OKLCH color space
31
+ # @note This is a two-step conversion process: first from sRGB to OKLab, then from OKLab to OKLCH
32
+ # @see SrgbToOklab
33
+ # @see OklabToOklch
9
34
  def self.convert(srgb_color)
10
35
  # First convert sRGB to OKLab
11
36
  oklab_color = SrgbToOklab.convert(srgb_color)
@@ -15,4 +40,4 @@ module Abachrome
15
40
  end
16
41
  end
17
42
  end
18
- end
43
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
1
+ #
2
2
 
3
3
  module Abachrome
4
4
  module Gamut
@@ -69,4 +69,4 @@ module Abachrome
69
69
  end
70
70
  end
71
71
  end
72
- end
72
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
1
+ #
2
2
 
3
3
  module Abachrome
4
4
  module Gamut
@@ -22,4 +22,4 @@ module Abachrome
22
22
 
23
23
  register(P3.new)
24
24
  end
25
- end
25
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
1
+ #
2
2
 
3
3
  lib
4
4
  abachrome
@@ -20,4 +20,4 @@ module Abachrome
20
20
 
21
21
  register(Rec2020.new)
22
22
  end
23
- end
23
+ end