Module: Abachrome::ColorMixins::ToColorspace

Defined in:
lib/abachrome/color_mixins/to_colorspace.rb

Instance Method Summary collapse

Instance Method Details

#convert_to(space_name) ⇒ Abachrome::Color

Convert this color to a different color space.

Convert a color from sRGB to OKLCH

rgb_color.convert_to(:oklch)

Parameters:

  • space_name (String, Symbol)

    The name of the target color space to convert to.

Returns:

See Also:



64
65
66
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 64

def convert_to(space_name)
  to_color_space(ColorSpace.find(space_name))
end

#convert_to!(space_name) ⇒ Abachrome::Color

Converts this color to the specified color space in place.

red = Abachrome::Color.new(1, 0, 0, color_space: :srgb) red.convert_to!(:oklch) # Converts the red color to OKLCH space in place

Parameters:

  • space_name (String, Symbol)

    The name or identifier of the target color space to convert to.

Returns:

  • (Abachrome::Color)

    Returns self with its values converted to the specified color space.

Raises:

  • (Abachrome::ColorSpaceNotFoundError)

    If the specified color space is not registered.

See Also:



77
78
79
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 77

def convert_to!(space_name)
  to_color_space!(ColorSpace.find(space_name))
end

#in_color_space(space_name) ⇒ Abachrome::Color

Convert a color to a specified color space.

red_rgb = Abachrome::Color.new(:srgb, [1, 0, 0]) red_lch = red_rgb.in_color_space(:oklch)

Parameters:

  • space_name (Symbol, String)

    The name of the color space to convert to.

Returns:



88
89
90
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 88

def in_color_space(space_name)
  convert_to(space_name)
end

#in_color_space!(space_name) ⇒ Abachrome::Color

Converts the color to the specified color space and mutates the current object.

color = Abachrome::Color.new([255, 0, 0], :rgb) color.in_color_space!(:oklch) # Color is now in OKLCH space

Parameters:

  • space_name (Symbol, String)

    The target color space to convert to (e.g., :oklch, :rgb, :lab)

Returns:

See Also:



100
101
102
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 100

def in_color_space!(space_name)
  convert_to!(space_name)
end

#to_color_space(target_space) ⇒ Abachrome::Color

Converts the current color to the specified target color space.

This method transforms the current color into an equivalent color in a different color space. If the target space is the same as the current color space, no conversion is performed and the current color is returned.

if the target space is the same as the current color space

Parameters:

Returns:



33
34
35
36
37
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 33

def to_color_space(target_space)
  return self if color_space == target_space

  Converter.convert(self, target_space.name)
end

#to_color_space!(target_space) ⇒ Abachrome::Color

Converts the color object to the specified target color space in-place. This method modifies the current object by changing its color space and coordinates to match the target color space.

Parameters:

Returns:

See Also:



46
47
48
49
50
51
52
53
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 46

def to_color_space!(target_space)
  unless color_space == target_space
    converted = to_color_space(target_space)
    @color_space = converted.color_space
    @coordinates = converted.coordinates
  end
  self
end