Class: PRRD::Color
- Inherits:
-
Object
- Object
- PRRD::Color
- Defined in:
- lib/prrd.rb
Overview
Color class
Instance Attribute Summary collapse
-
#collection ⇒ Object
Accessors.
-
#hexcode ⇒ Object
Accessors.
Instance Method Summary collapse
-
#darken(percentage) ⇒ String
Darken the color.
-
#initialize(name, tint = nil, alpha = nil) ⇒ Color
constructor
Constructor.
-
#lighten(percentage) ⇒ Object
Lighten the color.
-
#to_hex(name, tint = nil, alpha = nil) ⇒ Object
Translate into hexcode.
-
#to_s ⇒ Object
String representation.
Constructor Details
#initialize(name, tint = nil, alpha = nil) ⇒ Color
Constructor
61 62 63 64 65 66 67 |
# File 'lib/prrd.rb', line 61 def initialize(name, tint = nil, alpha = nil) if name.is_a?(String) && name.include?('#') @hexcode = name else @hexcode = to_hex(name, tint, alpha) end end |
Instance Attribute Details
#collection ⇒ Object
Accessors
55 56 57 |
# File 'lib/prrd.rb', line 55 def collection @collection end |
#hexcode ⇒ Object
Accessors
55 56 57 |
# File 'lib/prrd.rb', line 55 def hexcode @hexcode end |
Instance Method Details
#darken(percentage) ⇒ String
Darken the color
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/prrd.rb', line 99 def darken(percentage) fail 'Can not operate on a non-hexadecimal color' if @hexcode.nil? hexcode = @hexcode.gsub('#', '') percentage = percentage.to_f / 100 rgb = hexcode.scan(/../).map { |c| c.hex } rgb.map! { |c| (c.to_i * percentage).round } "#%02x%02x%02x" % rgb end |
#lighten(percentage) ⇒ Object
Lighten the color
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/prrd.rb', line 112 def lighten(percentage) fail 'Can not operate on a non-hexadecimal color' if @hexcode.nil? hexcode = @hexcode.gsub('#', '') percentage = percentage.to_f / 100 rgb = hexcode.scan(/../).map { |c| c.hex } rgb.map! { |c| [(c.to_i + 255 * percentage).round, 255].min } "#%02x%02x%02x" % rgb end |
#to_hex(name, tint = nil, alpha = nil) ⇒ Object
Translate into hexcode
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/prrd.rb', line 74 def to_hex(name, tint = nil, alpha = nil) if @collection.nil? @collection = { red: {light: '#EA644A', dark: '#CC3118'}, orange: {light: '#EC9D48', dark: '#CC7016'}, yellow: {light: '#ECD748', dark: '#C9B215'}, green: {light: '#54EC48', dark: '#24BC14'}, blue: {light: '#48C4EC', dark: '#1598C3'}, pink: {light: '#DE48EC', dark: '#B415C7'}, purple: {light: '#7648EC', dark: '#4D18E4'} } end name = name.to_sym tint = tint.nil? ? :light : tint.to_sym fail "Unknown color #{name}" unless @collection.key? name fail "Unknown tint #{tint}" unless @collection[name].key? tint alpha.nil? ? @collection[name][tint] : @collection[name][tint] + alpha end |
#to_s ⇒ Object
String representation
124 125 126 |
# File 'lib/prrd.rb', line 124 def to_s @hexcode end |