class Pigment::Color::RGB

Represent a color in the RGB Format

Attributes

a[R]

@return [Float]

alpha[R]

@return [Float]

b[R]

@return [Float]

blue[R]

@return [Float]

g[R]

@return [Float]

green[R]

@return [Float]

r[R]

@return [Float]

red[R]

@return [Float]

Public Class Methods

convert(color) click to toggle source

Converts a color into RGB format from any possible format @param [Pigment::Color] color @return [Pigment::Color] @raise [InvalidColorFormatError]

# File lib/pigment/color/rgb.rb, line 17
def convert(color)
  case color
  when RGB then color
  when HSL then from_hsl(*color)
  else raise InvalidColorFormatError, color
  end
end
from_hex(hex) click to toggle source

Creates a Pigment::Color::RGB from an HTML Hex code String @param [String, Integer, Float] hex @return [Pigment::Color::RGB] @raise [InvalidColorFormatError]

# File lib/pigment/color/rgb.rb, line 29
def from_hex(hex)
  case hex
  when String  then from_hex_string(hex)
  when Integer then from_hex_integer(hex)
  when Float   then from_hex_integer(hex.round)
  else raise InvalidColorFormatError, hex
  end
end
from_rgb_integers(red, green, blue) click to toggle source

@param [Integer] red @param [Integer] green @param [Integer] blue @return [Pigment::Color::RGB]

# File lib/pigment/color/rgb.rb, line 57
def from_rgb_integers(red, green, blue)
  from_rgba_integers(red, green, blue)
end
from_rgba_integers(red, green, blue, alpha = 255) click to toggle source

Creates a Pigment::Color from a group of rgba Integers @param [Integer] red @param [Integer] green @param [Integer] blue @param [Integer] alpha @return [Pigment::Color::RGB] @raise [InvalidColorFormatError]

# File lib/pigment/color/rgb.rb, line 45
def from_rgba_integers(red, green, blue, alpha = 255)
  color = [red, green, blue, alpha]
  raise InvalidColorFormatError, color unless color.all? do |c|
    (0..255).include? c
  end
  new(*color.map { |c| c / 255.0 })
end
new(red, green, blue, alpha = 1.0) click to toggle source

Pigment uses sRGB or sRGBA as the default color system Pigment::Color::RGB is represented as an array of floats, which are ranged from 0.0 to 1.0 @param [Float, Integer] red between 0.0 and 1.0 @param [Float, Integer] green between 0.0 and 1.0 @param [Float, Integer] blue between 0.0 and 1.0 @param [Float, Integer] alpha between 0.0 and 1.0 @return [Pigment::Color::RGB] @raise [InvalidColorFormatError]

# File lib/pigment/color/rgb.rb, line 131
def initialize(red, green, blue, alpha = 1.0)
  @red   = red.to_f.snap
  @green = green.to_f.snap
  @blue  = blue.to_f.snap
  @alpha = alpha.to_f

  color = to_floats(with_alpha: true)
  raise InvalidColorFormatError, color unless color.all? { |c| c.between?(0.0, 1.0) }
end
random() click to toggle source

@return [Pigment::Color::RGB] a random generated color

# File lib/pigment/color/rgb.rb, line 62
def random
  new(rand(0.0..1.0), rand(0.0..1.0), rand(0.0..1.0))
end
suppress(color) click to toggle source

Suppress an array of floats by dividing by the greatest color component. @param [Array] color @return [Array]

# File lib/pigment/color/rgb.rb, line 69
def suppress(color)
  return color.map { |c| c / color.max } unless color.max.between?(0.0, 1.0)
  color
end

Private Class Methods

from_hex_integer(hex) click to toggle source

@param [Integer] hex @return [Pigment::Color::RGB]

# File lib/pigment/color/rgb.rb, line 77
def from_hex_integer(hex)
  raise InvalidColorFormatError, hex unless hex.is_a?(Numeric) && hex.between?(0, 0xFFFFFFFF)
  red   = (hex >> 24 & 0xFF)
  green = (hex >> 16 & 0xFF)
  blue  = (hex >>  8 & 0xFF)
  alpha = (hex       & 0xFF)
  from_rgba_integers(red, green, blue, alpha)
end
from_hex_string(hex) click to toggle source

@param [String] hex @raise [InvalidColorFormatError] @return [Pigment::Color::RGB]

# File lib/pigment/color/rgb.rb, line 89
def from_hex_string(hex)
  matches = hex.match(/^#?(?<r>\h{2})(?<g>\h{2})(?<b>\h{2})(?<a>\h{2})?$/)&.named_captures
  raise InvalidColorFormatError, hex unless matches
  matches["a"] ||= 'FF'
  new(*matches.values.map { |value| value.to_i(16) / 255.0 })
end
from_hsl(hue, saturation, lightness, alpha = 1.0) click to toggle source

Creates a Pigment::Color::RGB form the HSL color System. It's mostly used to calculate harmonic colors. @param [#to_f] hue between 0.0 and 1.0 @param [#to_f] saturation between 0.0 and 1.0 @param [#to_f] lightness between 0.0 and 1.0 @param [#to_f] alpha between 0.0 and 1.0 @return [Pigment::Color::RGB]

# File lib/pigment/color/rgb.rb, line 102
def from_hsl(hue, saturation, lightness, alpha = 1.0)
  return new(lightness, lightness, lightness, alpha) if saturation == 0
  v2 = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - saturation * lightness
  v1 = 2 * lightness - v2
  color = [hue + (1 / 3.0), hue, hue - (1 / 3.0)].map do |hv|
    hv = hv < 0 ? hv + 1
        : hv > 1 ? hv - 1
        : hv

    case
    when 6 * hv < 1 then v1 + (v2 - v1) * 6 * hv
    when 2 * hv < 1 then v2
    when 3 * hv < 2 then v1 + (v2 - v1) * ((2 / 3.0) - hv) * 6
    else v1
    end
  end
  color << alpha
  new(*color.map { |c| c.round(2) })
end

Public Instance Methods

*(n) click to toggle source

Multiplies all the color components by n. If any component gets out of the 0 to 1.0 range it gets suppressed. @param [Numeric] n @return [Pigment::Color::RGB]

# File lib/pigment/color/rgb.rb, line 175
def *(n)
  raise ArgumentError, "Expecting Numeric. Given #{n.class}" unless n.is_a? Numeric
  n = rgb.map { |c| c * n.to_f }
  self.class.new(*self.class.suppress(n), @alpha)
end
+(color) click to toggle source

Sums all the two colors components. If any component gets out of the 0 to 1.0 range it gets suppressed. @param [Pigment::Color] color @return [Pigment::Color::RGB] @raise [InvalidColorFormatError]

# File lib/pigment/color/rgb.rb, line 145
def +(color)
  raise InvalidColorFormatError, color unless color.is_a?(Pigment::Color)
  color = color.into(self.class)
  color = [
      @red + color.red,
      @green + color.green,
      @blue + color.blue
  ]

  self.class.new(*self.class.suppress(color), @alpha)
end
-(color) click to toggle source

Subtracts all the two color components. If any component gets out of the 0 to 1.0 range it gets suppressed. Tone component will be 0 if it gets lower than 0 @param [Pigment::Color] color @return [Pigment::Color::RGB] @raise [InvalidColorFormatError]

# File lib/pigment/color/rgb.rb, line 162
def -(color)
  raise InvalidColorFormatError, color unless color.is_a?(Pigment::Color)
  color = color.into(self.class)
  self.class.new(*self.class.suppress([
      @red - color.red,
      @green - color.green,
      @blue - color.blue
  ].map { |c| c >= 0 ? c : 0 }), @alpha)
end
/(n) click to toggle source

Divides all the color components by n. If any component gets out of the 0 to 1.0 range it gets suppressed. @param [Numeric] n @return [Pigment::Color::RGB]

# File lib/pigment/color/rgb.rb, line 184
def /(n)
  raise ArgumentError, "Expecting Numeric. Given #{n.class}" unless n.is_a? Numeric
  n =  rgb.map { |c| c / n.to_f }
  self.class.new(*self.class.suppress(n), @alpha)
end
==(other) click to toggle source

Test if two colors are equal @param [Pigment::Color] other @return [Boolean]

# File lib/pigment/color/rgb.rb, line 193
def ==(other)
  return false unless other.is_a?(Pigment::Color)
  other = Pigment::Color::RGB.convert(other)
  other.red.snap       == @red.snap &&
      other.blue.snap  == @blue.snap &&
      other.green.snap == @green.snap &&
      other.alpha.snap == @alpha.snap
end
grayscale() click to toggle source

@return [Pigment::Color::RGB] the grayscale correspondent of the color

# File lib/pigment/color/rgb.rb, line 203
def grayscale
  gray = (@red + @green + @blue) / 3.0
  self.class.new(gray, gray, gray, @alpha)
end
grayscale?() click to toggle source

@return [Boolean] true if all components are the same, false otherwise

# File lib/pigment/color/rgb.rb, line 209
def grayscale?
  @red == @green && @green == @blue
end
interpolate(color, amount = 0.5) click to toggle source

Interpolates two colors. Amount must be an float between -1.0 and 1.0 @param [Pigment::Color] color @param [Float] amount @return [Pigment::Color::RGB] @raise [InvalidColorFormatError]

# File lib/pigment/color/rgb.rb, line 218
def interpolate(color, amount = 0.5)
  raise InvalidColorFormatError, color unless color.is_a?(Pigment::Color)
  raise ArgumentError, "Amount must be an float between -1.0 and 1.0, got #{amount}" unless (-1.0..1.0).include?(amount)
  color = color.into(Pigment::Color::RGB)
  n = [rgb, color.rgb].transpose.map! { |c, d| c + amount * (d - c) }
  self.class.new(*self.class.suppress(n))
end
Also aliased as: mix
inverse() click to toggle source

@return [RGB] the Invert color

# File lib/pigment/color/rgb.rb, line 227
def inverse
  self.class.new(*rgb.map { |c| 1.0 - c }, @alpha)
end
mix(color, amount = 0.5)
Alias for: interpolate
rgb() click to toggle source

@return [Array<Float>] an array with the red, green and blue color components

# File lib/pigment/color/rgb.rb, line 257
def rgb
  to_a(with_alpha: false)
end
rgba(with_alpha: true)
Alias for: to_a
to_a(with_alpha: true) click to toggle source

@param [Boolean] with_alpha @return [Array<Float>] an array of the color components. Alpha value is passed as well if with_alpha is set to true.

# File lib/pigment/color/rgb.rb, line 239
def to_a(with_alpha: true)
  with_alpha ? [@red, @green, @blue, @alpha] : [@red, @green, @blue]
end
Also aliased as: rgba
to_hex(with_alpha: true) click to toggle source

@param [Boolean] with_alpha @return [String] an hexadecimal representation of the color components. Alpha value is passed as well if

with_alpha is set to true.
# File lib/pigment/color/rgb.rb, line 252
def to_hex(with_alpha: true)
  to_ints(with_alpha: with_alpha).map { |v| '%02x' % v }.join
end
to_ints(with_alpha: true) click to toggle source

@param [Boolean] with_alpha @return [Array<Integer>] an array of the color components. Alpha value is passed as well if with_alpha is set to true.

# File lib/pigment/color/rgb.rb, line 245
def to_ints(with_alpha: true)
  to_a(with_alpha: with_alpha).map { |v| (v * 255).to_i }
end
to_s() click to toggle source

@return [String] the string representation of a hsl color

# File lib/pigment/color/rgb.rb, line 262
def to_s
  "RGB Color(red: #{red}, green: #{green}, blue: #{blue}, alpha: #{alpha})"
end
triadic() click to toggle source

@return [Array<Pigment::Color>] An array of the triadic colors from self

# File lib/pigment/color/rgb.rb, line 233
def triadic
  [self.class.new(@blue, @red, @green, @alpha), self.class.new(@green, @blue, @red, @alpha)]
end

Private Instance Methods

method_missing(method, *args) click to toggle source

@return [Array<Float>] an array with the respective rgba components

Calls superclass method
# File lib/pigment/color/rgb.rb, line 277
def method_missing(method, *args)
  return super unless method =~ /^[abgr]+$/ && args.empty?
  method.size.times.map{ |i| send(method[i]) }
end