Class: ColorFun::RGB

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue) ⇒ RGB

Initialize by passinga red, green and blue hex code



8
9
10
# File 'lib/color_fun/rgb.rb', line 8

def initialize(red, green, blue)
  @red, @green, @blue = red.to_i, green.to_i, blue.to_i
end

Instance Attribute Details

#blueObject

Accessors for accessing the individual colors



5
6
7
# File 'lib/color_fun/rgb.rb', line 5

def blue
  @blue
end

#greenObject

Accessors for accessing the individual colors



5
6
7
# File 'lib/color_fun/rgb.rb', line 5

def green
  @green
end

#redObject

Accessors for accessing the individual colors



5
6
7
# File 'lib/color_fun/rgb.rb', line 5

def red
  @red
end

Class Method Details

.blueObject



59
# File 'lib/color_fun/rgb.rb', line 59

def self.blue; self.new(0,0,255); end

.from_hex(hex) ⇒ Object

Initialize a new color object for a given hex code



52
53
54
# File 'lib/color_fun/rgb.rb', line 52

def self.from_hex(hex)
  self.new(*hex.scan(/../).map { |i| i.to_i(16) })
end

.greenObject



58
# File 'lib/color_fun/rgb.rb', line 58

def self.green; self.new(0,255,0); end

.redObject

Methods to create raw red, green and blue colors



57
# File 'lib/color_fun/rgb.rb', line 57

def self.red; self.new(255,0,0); end

Instance Method Details

#dark?Boolean

Return whether or not the color is visibly dark to the human eye?

Returns:

  • (Boolean)


18
19
20
# File 'lib/color_fun/rgb.rb', line 18

def dark?
  perceived_brightness < 160
end

#darker(percent = 50) ⇒ Object

Return a new color object for a darker version of this color



34
35
36
37
# File 'lib/color_fun/rgb.rb', line 34

def darker(percent = 50)
  percent = ((100 - percent) / 100.0)
  RGB.new(@red * percent, @green * percent, @blue * percent)
end

#hexObject

Return a hex string for this color



40
41
42
43
44
# File 'lib/color_fun/rgb.rb', line 40

def hex
  [@red, @green, @blue].map do |c|
    c.to_s(16).rjust(2, '0')
  end.join
end

#hslObject

Return a HSL version of this color



47
48
49
# File 'lib/color_fun/rgb.rb', line 47

def hsl
  @hsl ||= HSL.from_rbg(@red, @green, @blue)
end

#light?Boolean

Return whether or not the color is visibly light to the human eye?

Returns:

  • (Boolean)


23
24
25
# File 'lib/color_fun/rgb.rb', line 23

def light?
  perceived_brightness > 161
end

#lighter(percent = 50) ⇒ Object

Return a new color object for a lighter version of this color



28
29
30
31
# File 'lib/color_fun/rgb.rb', line 28

def lighter(percent = 50)
  percent = (percent / 100.0)
  RGB.new((255 - @red) * percent + @red, (255 - @green) * percent + @green, (255 - @blue) * percent + @blue)
end

#perceived_brightnessObject

Return the brightness value for a given colour (HSP)



13
14
15
# File 'lib/color_fun/rgb.rb', line 13

def perceived_brightness
  @brightness ||= Math.sqrt((0.299 * @red * @red) + (0.587 * @green * @green) + (0.114 * @blue * @blue))
end