Class: Abachrome::Converter
- Inherits:
-
Object
- Object
- Abachrome::Converter
- Defined in:
- lib/abachrome/converter.rb
Class Method Summary collapse
-
.convert(color, to_space_name) ⇒ Abachrome::Color
Converts a color from its current color space to the specified target color space.
-
.register(from_space, to_space, converter_class) ⇒ Class
Register a converter class for transforming colors between two specific color spaces.
-
.register_all_converters ⇒ void
private
converter = Abachrome::Converter.new converter.register_all_converters.
-
.registry ⇒ Hash
Returns the registry hash used to store color space converters.
Class Method Details
.convert(color, to_space_name) ⇒ Abachrome::Color
Converts a color from its current color space to the specified target color space.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/abachrome/converter.rb', line 53 def convert(color, to_space_name) to_space = ColorSpace.find(to_space_name) return color if color.color_space == to_space # convert model first to_model = to_space.color_model converter = find_converter(color.color_space.color_model, to_model.to_s) raise "No converter found from #{color.color_space.color_model} to #{to_model}" unless converter converter.convert(color) end |
.register(from_space, to_space, converter_class) ⇒ Class
Register a converter class for transforming colors between two specific color spaces.
43 44 45 |
# File 'lib/abachrome/converter.rb', line 43 def register(from_space, to_space, converter_class) registry[[from_space.to_s, to_space.to_s]] = converter_class end |
.register_all_converters ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
converter = Abachrome::Converter.new converter.register_all_converters
Automatically registers all converter classes found in the Converters namespace. The method iterates through constants in the Converters module, identifies classes with naming pattern "FromSpaceToSpace" (e.g., LrgbToOklab), extracts the source and destination color spaces from the class name, and registers the converter class for the corresponding color space conversion.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/abachrome/converter.rb', line 78 def register_all_converters Converters.constants.each do |const_name| const = Converters.const_get(const_name) next unless const.is_a?(Class) # Parse from_space and to_space from class name (e.g., LrgbToOklab) next unless const_name.to_s =~ /^(.+)To(.+)$/ from_space = ::Regexp.last_match(1).downcase.to_sym to_space = ::Regexp.last_match(2).downcase.to_sym # Register the converter register(from_space, to_space, const) end end |
.registry ⇒ Hash
Returns the registry hash used to store color space converters.
This method lazily initializes and returns the hash used internally to store converter mappings between different color spaces. This registry is a central repository that maps color space pairs to their appropriate conversion functions.
33 34 35 |
# File 'lib/abachrome/converter.rb', line 33 def registry @registry ||= {} end |