Class: Abachrome::Converters::Base
- Inherits:
-
Object
- Object
- Abachrome::Converters::Base
- Defined in:
- lib/abachrome/converters/base.rb
Direct Known Subclasses
LmsToLrgb, LmsToSrgb, LmsToXyz, LrgbToOklab, LrgbToSrgb, LrgbToXyz, OklabToLms, OklabToLrgb, OklabToOklch, OklabToSrgb, OklchToLrgb, OklchToOklab, OklchToSrgb, OklchToXyz, SrgbToOklch, XyzToLms, XyzToOklab
Instance Attribute Summary collapse
-
#from_space ⇒ Object
readonly
Returns the value of attribute from_space.
-
#to_space ⇒ Object
readonly
Returns the value of attribute to_space.
Class Method Summary collapse
-
.convert(color, to_space) ⇒ Abachrome::Color
Converts a color from its current color space to a target color space.
-
.find_converter(from_space_id, to_space_id) ⇒ Converter?
Find a converter for converting between color spaces.
-
.raise_unless(color, model) ⇒ nil
Validates that a color uses the expected color model.
-
.register(from_space_id, to_space_id, converter_class) ⇒ void
Register a converter class for transforming colors between two specific color spaces.
Instance Method Summary collapse
-
#can_convert?(color) ⇒ Boolean
Determines if the converter can handle the given color.
-
#convert(color) ⇒ Abachrome::Color
abstract
Converts a color from one color space to another.
-
#initialize(from_space, to_space) ⇒ Abachrome::Converters::Base
constructor
Initialize a new converter between two color spaces.
Constructor Details
#initialize(from_space, to_space) ⇒ Abachrome::Converters::Base
Initialize a new converter between two color spaces.
29 30 31 32 |
# File 'lib/abachrome/converters/base.rb', line 29 def initialize(from_space, to_space) @from_space = from_space @to_space = to_space end |
Instance Attribute Details
#from_space ⇒ Object (readonly)
Returns the value of attribute from_space.
22 23 24 |
# File 'lib/abachrome/converters/base.rb', line 22 def from_space @from_space end |
#to_space ⇒ Object (readonly)
Returns the value of attribute to_space.
22 23 24 |
# File 'lib/abachrome/converters/base.rb', line 22 def to_space @to_space end |
Class Method Details
.convert(color, to_space) ⇒ Abachrome::Color
Converts a color from its current color space to a target color space.
This method finds the appropriate converter class for the given source and target color spaces and performs the conversion.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/abachrome/converters/base.rb', line 98 def self.convert(color, to_space) converter_class = find_converter(color.color_space.id, to_space.id) unless converter_class raise ConversionError, "No converter found from #{color.color_space.name} to #{to_space.name}" end converter = converter_class.new(color.color_space, to_space) converter.convert(color) end |
.find_converter(from_space_id, to_space_id) ⇒ Converter?
Find a converter for converting between color spaces.
84 85 86 87 |
# File 'lib/abachrome/converters/base.rb', line 84 def self.find_converter(from_space_id, to_space_id) @converters ||= {} @converters[[from_space_id, to_space_id]] end |
.raise_unless(color, model) ⇒ nil
Validates that a color uses the expected color model.
50 51 52 53 54 |
# File 'lib/abachrome/converters/base.rb', line 50 def self.raise_unless(color, model) return if color.color_space.color_model == model raise "#{color} is #{color.color_space.color_model}), expecting #{model}" end |
.register(from_space_id, to_space_id, converter_class) ⇒ void
This method returns an undefined value.
Register a converter class for transforming colors between two specific color spaces.
74 75 76 77 |
# File 'lib/abachrome/converters/base.rb', line 74 def self.register(from_space_id, to_space_id, converter_class) @converters ||= {} @converters[[from_space_id, to_space_id]] = converter_class end |
Instance Method Details
#can_convert?(color) ⇒ Boolean
Determines if the converter can handle the given color.
This method checks if the color’s current color space matches the converter’s source color space.
false otherwise
64 65 66 |
# File 'lib/abachrome/converters/base.rb', line 64 def can_convert?(color) color.color_space == from_space end |
#convert(color) ⇒ Abachrome::Color
This is an abstract method that must be implemented by subclasses.
Converts a color from one color space to another.
40 41 42 |
# File 'lib/abachrome/converters/base.rb', line 40 def convert(color) raise NotImplementedError, "Subclasses must implement #convert" end |