Class: Abachrome::Converters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/abachrome/converters/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_space, to_space) ⇒ Abachrome::Converters::Base

Initialize a new converter between two color spaces.

Parameters:



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_spaceObject (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_spaceObject (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.

Parameters:

Returns:

Raises:

  • (ConversionError)

    If no converter is found for the given color spaces



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.

Parameters:

  • from_space_id (Symbol, String)

    The identifier of the source color space

  • to_space_id (Symbol, String)

    The identifier of the destination color space

Returns:

  • (Converter, nil)

    The converter instance for the specified color spaces, or nil if no converter is found



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.

Parameters:

  • color (Abachrome::Color)

    The color object to check

  • model (Symbol, String)

    The expected color model

Returns:

  • (nil)

    If the color’s model matches the expected model

Raises:

  • (RuntimeError)

    If the color’s model doesn’t match the expected 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.

Parameters:

  • from_space_id (Symbol)

    The identifier of the source color space

  • to_space_id (Symbol)

    The identifier of the destination color space

  • converter_class (Class)

    The converter class that handles the transformation



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

Parameters:

Returns:

  • (Boolean)

    true if the converter can convert from the color’s current color space,



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 method is abstract.

This is an abstract method that must be implemented by subclasses.

Converts a color from one color space to another.

Parameters:

Returns:

Raises:

  • (NotImplementedError)

    If the subclass doesn’t implement this method



40
41
42
# File 'lib/abachrome/converters/base.rb', line 40

def convert(color)
  raise NotImplementedError, "Subclasses must implement #convert"
end