class Colors::RGB

Attributes

b[R]
blue[R]
g[R]
green[R]
r[R]
red[R]

Public Class Methods

new(r, g, b) click to toggle source
# File lib/colors/rgb.rb, line 25
def initialize(r, g, b)
  @r, @g, @b = canonicalize(r, g, b)
end
parse(hex_string) click to toggle source
# File lib/colors/rgb.rb, line 5
def self.parse(hex_string)
  error_message = "must be a hexadecimal string of `#rrggbb` or `#rgb` form"
  unless hex_string.respond_to?(:to_str)
    raise ArgumentError, "#{error_message}: #{hex_string.inspect}"
  end

  hex_string = hex_string.to_str
  hexes = hex_string.match(/\A#(\h+)\z/) { $1 }
  case hexes&.length
  when 3  # rgb
    r, g, b = hexes.scan(/\h/).map {|h| h.hex * 17 }
    new(r, g, b)
  when 6  # rrggbb
    r, g, b = hexes.scan(/\h{2}/).map(&:hex)
    new(r, g, b)
  else
    raise ArgumentError, "#{error_message}: #{hex_string.inspect}"
  end
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/colors/rgb.rb, line 57
def ==(other)
  case other
  when RGBA
    other == self
  when RGB
    r == other.r && g == other.g && b == other.b
  else
    super
  end
end
b=(b) click to toggle source
# File lib/colors/rgb.rb, line 45
def b=(b)
  @b = canonicalize_component(b, :b)
end
Also aliased as: blue=
blue=(b)
Alias for: b=
components() click to toggle source
# File lib/colors/rgb.rb, line 31
def components
  [r, g, b]
end
Also aliased as: rgb_components
desaturate(factor) click to toggle source
# File lib/colors/rgb.rb, line 68
def desaturate(factor)
  to_hsl.desaturate(factor).to_rgb
end
g=(g) click to toggle source
# File lib/colors/rgb.rb, line 41
def g=(g)
  @g = canonicalize_component(g, :g)
end
Also aliased as: green=
green=(g)
Alias for: g=
hsl_components() click to toggle source
# File lib/colors/rgb.rb, line 94
def hsl_components
  m1, m2 = [r, g, b].minmax
  c = m2 - m1
  hh = case
       when c == 0
         0r
       when m2 == r
         ((g - b) / c) % 6r
       when m2 == g
         ((b - r) / c + 2) % 6r
       when m2 == b
         ((r - g) / c + 4) % 6r
       end
  h = 60r * hh
  l = 0.5r * m2 + 0.5r * m1
  s = if l == 1 || l == 0
        0r
      else
        c / (1 - (2*l - 1).abs)
      end
  [h, s, l]
end
r=(r) click to toggle source
# File lib/colors/rgb.rb, line 37
def r=(r)
  @r = canonicalize_component(r, :r)
end
Also aliased as: red=
red=(r)
Alias for: r=
rgb_components()
Alias for: components
to_hex_string() click to toggle source
# File lib/colors/rgb.rb, line 72
def to_hex_string
  "##{components.map {|c| "%02x" % (255*c).round.to_i }.join('')}"
end
to_hsl() click to toggle source
# File lib/colors/rgb.rb, line 85
def to_hsl
  HSL.new(*hsl_components)
end
to_hsla(alpha: 1.0) click to toggle source
# File lib/colors/rgb.rb, line 89
def to_hsla(alpha: 1.0)
  alpha = canonicalize_component(alpha, :alpha)
  HSLA.new(*hsl_components, alpha)
end
to_husl() click to toggle source
# File lib/colors/rgb.rb, line 117
def to_husl
  c = RGB.new(r, g, b).to_xyz
  l, u, v = c.luv_components(WHITE_POINT_D65)
  h, s, l = Convert.luv_to_husl(l, u, v)
  HUSL.new(h, s.to_r.clamp(0r, 1r), l.to_r.clamp(0r, 1r))
end
to_rgb() click to toggle source
# File lib/colors/rgb.rb, line 76
def to_rgb
  self
end
to_rgba(alpha: 1.0) click to toggle source
# File lib/colors/rgb.rb, line 80
def to_rgba(alpha: 1.0)
  alpha = canonicalize_component(alpha, :alpha)
  RGBA.new(r, g, b, alpha)
end
to_xyz() click to toggle source
# File lib/colors/rgb.rb, line 124
def to_xyz
  XYZ.new(*Convert.rgb_to_xyz(r, g, b))
end

Private Instance Methods

canonicalize(r, g, b) click to toggle source
# File lib/colors/rgb.rb, line 128
        def canonicalize(r, g, b)
  if [r, g, b].map(&:class) == [Integer, Integer, Integer]
    canonicalize_from_integer(r, g, b)
  else
    [
      canonicalize_component_to_rational(r, :r),
      canonicalize_component_to_rational(g, :g),
      canonicalize_component_to_rational(b, :b)
    ]
  end
end
canonicalize_from_integer(r, g, b) click to toggle source
# File lib/colors/rgb.rb, line 140
        def canonicalize_from_integer(r, g, b)
  check_type(r, Integer, :r)
  check_type(g, Integer, :g)
  check_type(b, Integer, :b)
  [
    canonicalize_component_from_integer(r, :r),
    canonicalize_component_from_integer(g, :g),
    canonicalize_component_from_integer(b, :b)
  ]
end