Module: Abachrome::ColorMixins::ToSrgb
- Defined in:
- lib/abachrome/color_mixins/to_srgb.rb
Instance Method Summary collapse
-
#blue ⇒ AbcDecimal
Returns the blue component of the color in sRGB color space.
-
#green ⇒ AbcDecimal
Returns the green component of the color in sRGB space.
-
#red ⇒ AbcDecimal
Returns the red component of the color in the sRGB color space.
-
#rgb_array ⇒ Array<Integer>
Returns an array of RGB values (0-255) for this color.
-
#rgb_hex ⇒ String
Returns a hexadecimal representation of this color in sRGB color space.
-
#rgb_values ⇒ Array<Abachrome::AbcDecimal>
Returns the RGB values of the color as coordinates in the sRGB color space.
-
#srgb_values ⇒ Array<AbcDecimal>
Returns the RGB color values in the sRGB color space.
-
#to_rgb ⇒ Abachrome::Color
Alias for #to_srgb method.
-
#to_rgb! ⇒ self
Converts the current color to sRGB color space in place.
-
#to_srgb ⇒ Abachrome::Color
Converts the current color to the sRGB color space.
-
#to_srgb! ⇒ Abachrome::Color
Converts the color to the sRGB color space, mutating the current object.
Instance Method Details
#blue ⇒ AbcDecimal
Returns the blue component of the color in sRGB color space.
This method converts the current color to sRGB if needed and extracts the third coordinate value (blue).
95 96 97 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 95 def blue to_srgb.coordinates[2] end |
#green ⇒ AbcDecimal
Returns the green component of the color in sRGB space.
This method converts the current color to sRGB color space if needed, then extracts the green component (second coordinate).
85 86 87 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 85 def green to_srgb.coordinates[1] end |
#red ⇒ AbcDecimal
Returns the red component of the color in the sRGB color space.
normalized between 0 and 1.
75 76 77 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 75 def red to_srgb.coordinates[0] end |
#rgb_array ⇒ Array<Integer>
Returns an array of RGB values (0-255) for this color.
This method converts the color to sRGB, then scales the component values from the 0-1 range to the 0-255 range commonly used in RGB color codes. Values are rounded to the nearest integer and clamped between 0 and 255.
values in the 0-255 range
122 123 124 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 122 def rgb_array to_srgb.coordinates.map { |c| (c * 255).round.clamp(0, 255) } end |
#rgb_hex ⇒ String
Returns a hexadecimal representation of this color in sRGB color space. Converts the color to sRGB, then formats it as a hexadecimal string.
the hexadecimal representations of the red, green, and blue components, each ranging from 00 to FF. color.rgb_hex #=> "#3a7bc8"
134 135 136 137 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 134 def rgb_hex r, g, b = rgb_array format("#%02x%02x%02x", r, g, b) end |
#rgb_values ⇒ Array<Abachrome::AbcDecimal>
Returns the RGB values of the color as coordinates in the sRGB color space.
110 111 112 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 110 def rgb_values to_srgb.coordinates end |
#srgb_values ⇒ Array<AbcDecimal>
Returns the RGB color values in the sRGB color space.
the red, green, and blue color components in the sRGB color space.
103 104 105 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 103 def srgb_values to_srgb.coordinates end |
#to_rgb ⇒ Abachrome::Color
Alias for #to_srgb method.
44 45 46 47 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 44 def to_rgb # assume they mean srgb to_srgb end |
#to_rgb! ⇒ self
Converts the current color to sRGB color space in place. This is an alias for #to_srgb! as RGB commonly refers to sRGB in web and design contexts.
66 67 68 69 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 66 def to_rgb! # assume they mean srgb to_srgb! end |
#to_srgb ⇒ Abachrome::Color
Converts the current color to the sRGB color space.
If the color is already in the sRGB color space, returns the color instance unchanged. Otherwise, performs a color space conversion from the current color space to sRGB.
or self if already in sRGB
35 36 37 38 39 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 35 def to_srgb return self if color_space.name == :srgb Converter.convert(self, :srgb) end |
#to_srgb! ⇒ Abachrome::Color
Converts the color to the sRGB color space, mutating the current object. If the color is already in sRGB space, this method does nothing.
52 53 54 55 56 57 58 59 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 52 def to_srgb! unless color_space.name == :srgb srgb_color = to_srgb @color_space = srgb_color.color_space @coordinates = srgb_color.coordinates end self end |