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



93
94
95
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 93

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



83
84
85
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 83

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,



73
74
75
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 73

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]



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_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



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

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

Returns:



108
109
110
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 108

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



101
102
103
# File 'lib/abachrome/color_mixins/to_srgb.rb', line 101

def srgb_values
  to_srgb.coordinates
end

#to_rgbAbachrome::Color

Alias for #to_srgb method.

Returns:



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.

Returns:

  • (self)

    Returns self after converting to sRGB



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



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.

Returns:



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