Module: Abachrome::ColorMixins::ToSrgb

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

Instance Method Summary collapse

Instance Method Details

#blueAbcDecimal

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).

Returns:

  • (AbcDecimal)

    The blue component value in sRGB space, typically in range 0-1



95
96
97
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 95

def blue
  to_srgb.coordinates[2]
end

#greenAbcDecimal

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).

Returns:

  • (AbcDecimal)

    The green component value in the sRGB color space, typically in the range 0-1



85
86
87
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 85

def green
  to_srgb.coordinates[1]
end

#redAbcDecimal

Returns the red component of the color in the sRGB color space.

normalized between 0 and 1.

Returns:

  • (AbcDecimal)

    The red component value in the sRGB color space,



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

def red
  to_srgb.coordinates[0]
end

#rgb_arrayArray<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

Returns:

  • (Array<Integer>)

    An array of three integers representing the [R, G, B]



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_hexString

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"

Returns:

  • (String)

    A string in the format "#RRGGBB" where RR, GG, and BB are



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_valuesArray<Abachrome::AbcDecimal>

Returns the RGB values of the color as coordinates in the sRGB color space.

Returns:



110
111
112
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 110

def rgb_values
  to_srgb.coordinates
end

#srgb_valuesArray<AbcDecimal>

Returns the RGB color values in the sRGB color space.

the red, green, and blue color components in the sRGB color space.

Returns:

  • (Array<AbcDecimal>)

    An array of three AbcDecimal values representing



103
104
105
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 103

def srgb_values
  to_srgb.coordinates
end

#to_rgbAbachrome::Color

Alias for #to_srgb method.

Returns:



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.

Returns:

  • (self)

    Returns self after converting to sRGB



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_srgbAbachrome::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

Returns:



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.

Returns:



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