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)



62
63
64
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 62

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

Raises:

  • (Abachrome::ColorSpaceNotFoundError)

    If the specified color space is not registered.

See Also:



75
76
77
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 75

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)



86
87
88
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 86

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

See Also:



98
99
100
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 98

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



31
32
33
34
35
# File 'lib/abachrome/color_mixins/to_colorspace.rb', line 31

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.



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

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