Class: ColorFun::RGB
- Inherits:
-
Object
- Object
- ColorFun::RGB
- Defined in:
- lib/color_fun/rgb.rb
Instance Attribute Summary collapse
-
#blue ⇒ Object
Accessors for accessing the individual colors.
-
#green ⇒ Object
Accessors for accessing the individual colors.
-
#red ⇒ Object
Accessors for accessing the individual colors.
Class Method Summary collapse
- .blue ⇒ Object
-
.from_hex(hex) ⇒ Object
Initialize a new color object for a given hex code.
- .green ⇒ Object
-
.red ⇒ Object
Methods to create raw red, green and blue colors.
Instance Method Summary collapse
-
#dark? ⇒ Boolean
Return whether or not the color is visibly dark to the human eye?.
-
#darker(percent = 50) ⇒ Object
Return a new color object for a darker version of this color.
-
#hex ⇒ Object
Return a hex string for this color.
-
#hsl ⇒ Object
Return a HSL version of this color.
-
#initialize(red, green, blue) ⇒ RGB
constructor
Initialize by passinga red, green and blue hex code.
-
#light? ⇒ Boolean
Return whether or not the color is visibly light to the human eye?.
-
#lighter(percent = 50) ⇒ Object
Return a new color object for a lighter version of this color.
-
#perceived_brightness ⇒ Object
Return the brightness value for a given colour (HSP).
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
#blue ⇒ Object
Accessors for accessing the individual colors
5 6 7 |
# File 'lib/color_fun/rgb.rb', line 5 def blue @blue end |
#green ⇒ Object
Accessors for accessing the individual colors
5 6 7 |
# File 'lib/color_fun/rgb.rb', line 5 def green @green end |
#red ⇒ Object
Accessors for accessing the individual colors
5 6 7 |
# File 'lib/color_fun/rgb.rb', line 5 def red @red end |
Class Method Details
.blue ⇒ Object
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 |
.green ⇒ Object
58 |
# File 'lib/color_fun/rgb.rb', line 58 def self.green; self.new(0,255,0); end |
.red ⇒ Object
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?
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 |
#hex ⇒ Object
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 |
#hsl ⇒ Object
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?
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_brightness ⇒ Object
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 |