Class: ColorPalette::Color::RGB

Inherits:
Object
  • Object
show all
Defined in:
lib/color_palette/color/rgb.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRGB

Creates a black RGB colour object. Please use class methods from_* to create other colors.



76
77
78
79
80
# File 'lib/color_palette/color/rgb.rb', line 76

def initialize
  self.r = 0.0
  self.g = 0.0
  self.b = 0.0
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



7
8
9
# File 'lib/color_palette/color/rgb.rb', line 7

def b
  @b
end

#gObject

Returns the value of attribute g.



7
8
9
# File 'lib/color_palette/color/rgb.rb', line 7

def g
  @g
end

#rObject

Returns the value of attribute r.



7
8
9
# File 'lib/color_palette/color/rgb.rb', line 7

def r
  @r
end

Class Method Details

.from_fractions(r = 0.0, g = 0.0, b = 0.0) ⇒ Object

Creates an RGB colour object from fractional values 0..1.



20
21
22
23
24
25
26
# File 'lib/color_palette/color/rgb.rb', line 20

def self.from_fractions(r=0.0, g=0.0, b=0.0)
  color = ColorPalette::Color::RGB.new
  color.r = r
  color.g = g
  color.b = b
  color
end

.from_increment_in_other_color(other_color, r_increment, g_increment, b_increment) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/color_palette/color/rgb.rb', line 50

def self.from_increment_in_other_color(other_color, r_increment, g_increment, b_increment)
  color = from_fractions(other_color.r, other_color.g, other_color.b)
  color.r = color.r + r_increment
  color.g = color.g + g_increment
  color.b = color.b + b_increment
  color
end

.from_integers(r = 0, g = 0, b = 0) ⇒ Object

Creates an RGB colour object from integer values 0..255.



15
16
17
# File 'lib/color_palette/color/rgb.rb', line 15

def self.from_integers(r=0, g=0, b=0)
  from_fractions(r/255.0, g/255.0, b/255.0)
end

.from_percentages(r = 0, g = 0, b = 0) ⇒ Object

Creates an RGB colour object from percentages 0..100.



10
11
12
# File 'lib/color_palette/color/rgb.rb', line 10

def self.from_percentages(r=0, g=0, b=0)
  from_fractions(r/100.0, g/100.0, b/100.0)
end

.from_string(color_string) ⇒ Object

Creates an RGB color object from a hex string (e.g.,

Color::RGB.from_string("bef")
Color::RGB.from_string("#bef")
Color::RGB.from_string("#dabbec")
Color::RGB.from_string("dabbec")


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/color_palette/color/rgb.rb', line 34

def self.from_string(color_string)
  # remove hashtag if necessary
  color_string = color_string.gsub(%r{[#;]}, '')
  
  case color_string.size 
  when 3
    colors = color_string.scan(/[0-9A-Fa-f]/).map { |color_value| (color_value * 2).to_i(16) }
  when 6
    colors = color_string.scan(/[0-9A-Fa-f]{2}/).map { |color_value| color_value.to_i(16) }
  else
    raise ArgumentError
  end

  from_integers(*colors)
end

Instance Method Details

#==(other_color) ⇒ Object

Compares the other colour to this one. This will report that two RGB colours are equivalent if all component values are within 1e-4 (0.0001) of each other.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/color_palette/color/rgb.rb', line 61

def ==(other_color)
  # The other color will be converted to RGB before comparison. If there is no #to_rgb
  # conversion, this will raise an exception.
  other_color = other_color.to_rgb

  if other_color.is_a?(ColorPalette::Color::RGB)
    return ((@r - other_color.r).abs <= 1e-4) && 
      ((@g - other_color.g).abs <= 1e-4) && 
      ((@b - other_color.b).abs <= 1e-4)
  end

  return false
end

#to_rgbObject



95
96
97
# File 'lib/color_palette/color/rgb.rb', line 95

def to_rgb
  self
end

#to_sObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/color_palette/color/rgb.rb', line 82

def to_s
  r_integer = (r * 255).round
  r_integer = 255 if r_integer > 255

  g_integer = (g * 255).round
  g_integer = 255 if g_integer > 255

  b_integer = (b * 255).round
  b_integer = 255 if b_integer > 255

  "#%02x%02x%02x" % [ r_integer, g_integer, b_integer ]
end