Class: FoggyMirror::Color
- Inherits:
-
Object
- Object
- FoggyMirror::Color
- Defined in:
- lib/foggy-mirror/color.rb
Constant Summary collapse
- RGB_WEIGHTS =
[0.299, 0.587, 0.114].freeze
- PADDED_RGB_HEX =
'%06x'
Instance Method Summary collapse
- #==(oth) ⇒ Object (also: #eql?)
- #blue ⇒ Object
- #brightness(weights: RGB_WEIGHTS) ⇒ Object
- #green ⇒ Object
- #hash ⇒ Object
-
#initialize(color) ⇒ Color
constructor
A new instance of Color.
- #inspect ⇒ Object
- #red ⇒ Object
- #to_i ⇒ Object
- #to_s(prefix: '#', shorthand: true) ⇒ Object
Constructor Details
#initialize(color) ⇒ Color
Returns a new instance of Color.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/foggy-mirror/color.rb', line 9 def initialize(color) if color.kind_of?(String) color = color[/[A-Z0-9]{6}/i].to_i(16) elsif color.kind_of?(Array) color = (color[0] << 16) + (color[1] << 8) + color[2] end color = color.to_i if color > 0xFFFFFF || color < 0 raise ArgumentError, "out of range color value (#{color < 0 ? '-' : ''}0x#{color.abs.to_s(16).upcase})" end @value = color.to_i end |
Instance Method Details
#==(oth) ⇒ Object Also known as: eql?
45 46 47 |
# File 'lib/foggy-mirror/color.rb', line 45 def ==(oth) oth.to_i == @value end |
#blue ⇒ Object
41 42 43 |
# File 'lib/foggy-mirror/color.rb', line 41 def blue @value & 0x0000FF end |
#brightness(weights: RGB_WEIGHTS) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/foggy-mirror/color.rb', line 25 def brightness(weights: RGB_WEIGHTS) Math.sqrt( weights[0] * red ** 2 + weights[1] * green ** 2 + weights[2] * blue ** 2 ).to_i end |
#green ⇒ Object
37 38 39 |
# File 'lib/foggy-mirror/color.rb', line 37 def green (@value & 0x00FF00) >> 8 end |
#hash ⇒ Object
50 51 52 |
# File 'lib/foggy-mirror/color.rb', line 50 def hash @value.hash end |
#inspect ⇒ Object
71 72 73 |
# File 'lib/foggy-mirror/color.rb', line 71 def inspect "#<#{self.class.name} #{to_s}>" end |
#red ⇒ Object
33 34 35 |
# File 'lib/foggy-mirror/color.rb', line 33 def red (@value & 0xFF0000) >> 16 end |
#to_i ⇒ Object
67 68 69 |
# File 'lib/foggy-mirror/color.rb', line 67 def to_i @value end |
#to_s(prefix: '#', shorthand: true) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/foggy-mirror/color.rb', line 54 def to_s(prefix: '#', shorthand: true) if shorthand return 'red' if @value == 0xFF0000 if (@value & 0xF0F0F0) == (@value & 0x0F0F0F) << 4 hex = to_s(prefix: '', shorthand: false) return prefix.to_s + hex[0] + hex[2] + hex[4] end end prefix.to_s + (PADDED_RGB_HEX % @value).upcase end |