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).
93 94 95 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 93 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).
83 84 85 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 83 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.
73 74 75 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 73 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
120 121 122 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 120 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”
132 133 134 135 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 132 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.
108 109 110 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 108 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.
101 102 103 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 101 def srgb_values to_srgb.coordinates end |
#to_rgb ⇒ Abachrome::Color
Alias for #to_srgb method.
42 43 44 45 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 42 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.
64 65 66 67 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 64 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
33 34 35 36 37 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 33 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.
50 51 52 53 54 55 56 57 |
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 50 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 |