Module: Abachrome::ColorMixins::ToColorspace
- Defined in:
- lib/abachrome/color_mixins/to_colorspace.rb
Instance Method Summary collapse
-
#convert_to(space_name) ⇒ Abachrome::Color
Convert this color to a different color space.
-
#convert_to!(space_name) ⇒ Abachrome::Color
Converts this color to the specified color space in place.
-
#in_color_space(space_name) ⇒ Abachrome::Color
Convert a color to a specified color space.
-
#in_color_space!(space_name) ⇒ Abachrome::Color
Converts the color to the specified color space and mutates the current object.
-
#to_color_space(target_space) ⇒ Abachrome::Color
Converts the current color to the specified target color space.
-
#to_color_space!(target_space) ⇒ Abachrome::Color
Converts the color object to the specified target color space in-place.
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)
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
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)
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
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
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.
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 |