Class: Colorable::Color
- Inherits:
-
Object
- Object
- Colorable::Color
- Includes:
- Comparable
- Defined in:
- lib/colorable/color.rb
Constant Summary collapse
- @@colorset =
{}
Instance Method Summary collapse
-
#*(other) ⇒ Object
Color multiplication.
-
#+(other) ⇒ Object
Color addition.
-
#-(other) ⇒ Object
Color subtruction.
-
#/(other) ⇒ Object
Color division.
- #<=>(other) ⇒ Object
- #_hex ⇒ Object
- #_hsb ⇒ Object
- #_name ⇒ Object
- #_rgb ⇒ Object
- #dark? ⇒ Boolean
- #hex ⇒ Object
- #hsb ⇒ Object (also: #hsv)
-
#info ⇒ Object
Returns information of the color object.
-
#initialize(arg) ⇒ Color
constructor
Create a Color object which has several representations of a color.
- #inspect ⇒ Object
-
#mode ⇒ Object
Returns a current output mode.
-
#mode=(mode) ⇒ Object
Set output mode.
- #name ⇒ Object
-
#next(n = 1) ⇒ Object
(also: #succ)
Returns a next color object in X11 colors.
-
#prev(n = 1) ⇒ Object
Returns a previous color object in X11 colors.
- #rgb ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(arg) ⇒ Color
Create a Color object which has several representations of a color.
arg can be:
String or Symbol of color name
String of HEX color
Array of RGB values
NAME, RGB, HSB, HEX objects
Color object has output mode, which is determined by arg type.
14 15 16 |
# File 'lib/colorable/color.rb', line 14 def initialize(arg) @name, @rgb, @hsb, @hex, @mode = set_variables(arg) end |
Instance Method Details
#*(other) ⇒ Object
Color multiplication
other should be a Color object. It applies multiply compositing with its RGBs.
152 153 154 |
# File 'lib/colorable/color.rb', line 152 def *(other) new_by_composed_rgb(:*, other) end |
#+(other) ⇒ Object
Color addition
other can be:
Color object: apply minimum compositing with its RGBs.
Array of values or Integer: addiction applies based on its color mode.
125 126 127 128 129 130 131 132 |
# File 'lib/colorable/color.rb', line 125 def +(other) case other when Color new_by_composed_rgb(:+, other) else self.class.new @mode + other end end |
#-(other) ⇒ Object
Color subtruction
other can be:
Color object: apply maximum compositing with its RGBs.
Array of values or Integer: subtruction applies based on its color mode.
139 140 141 142 143 144 145 146 |
# File 'lib/colorable/color.rb', line 139 def -(other) case other when Color new_by_composed_rgb(:-, other) else self.class.new @mode - other end end |
#/(other) ⇒ Object
Color division
other should be a Color object. It applies screen compositing with its RGBs.
160 161 162 |
# File 'lib/colorable/color.rb', line 160 def /(other) new_by_composed_rgb(:/, other) end |
#<=>(other) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/colorable/color.rb', line 92 def <=>(other) if [self.name, other.name].any?(&:empty?) self.rgb <=> other.rgb else self.name <=> other.name end end |
#_hex ⇒ Object
59 60 61 |
# File 'lib/colorable/color.rb', line 59 def _hex @hex ||= HEX.new _rgb.to_hex end |
#_hsb ⇒ Object
63 64 65 |
# File 'lib/colorable/color.rb', line 63 def _hsb @hsb ||= HSB.new *_rgb.to_hsb end |
#_name ⇒ Object
51 52 53 |
# File 'lib/colorable/color.rb', line 51 def _name @name end |
#_rgb ⇒ Object
55 56 57 |
# File 'lib/colorable/color.rb', line 55 def _rgb @rgb end |
#dark? ⇒ Boolean
116 117 118 |
# File 'lib/colorable/color.rb', line 116 def dark? !!DARK_COLORS.detect { |d| d == self.name } end |
#hex ⇒ Object
71 72 73 |
# File 'lib/colorable/color.rb', line 71 def hex _hex.to_s end |
#hsb ⇒ Object Also known as: hsv
79 80 81 |
# File 'lib/colorable/color.rb', line 79 def hsb _hsb.to_a end |
#info ⇒ Object
Returns information of the color object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/colorable/color.rb', line 40 def info { name: name, rgb: rgb, hsb: hsb, hex: hex, mode: mode, dark: dark? } end |
#inspect ⇒ Object
35 36 37 |
# File 'lib/colorable/color.rb', line 35 def inspect "#<%s '%s<%s/%s/%s>'>" % [self.class, _name, _rgb, _hsb, _hex] end |
#mode ⇒ Object
Returns a current output mode
19 20 21 |
# File 'lib/colorable/color.rb', line 19 def mode "#{@mode.class}"[/\w+$/].intern end |
#mode=(mode) ⇒ Object
Set output mode.
24 25 26 27 28 29 |
# File 'lib/colorable/color.rb', line 24 def mode=(mode) modes = [_rgb, _hsb, _name, _hex] @mode = modes.detect { |m| m.class.to_s.match /#{mode}/i } || begin raise ArgumentError, "Invalid mode given" end end |
#name ⇒ Object
67 68 69 |
# File 'lib/colorable/color.rb', line 67 def name _name.to_s end |
#next(n = 1) ⇒ Object Also known as: succ
Returns a next color object in X11 colors. The color sequence is determined by its color mode.
103 104 105 106 107 |
# File 'lib/colorable/color.rb', line 103 def next(n=1) @@colorset[mode] ||= Colorable::Colorset.new(order: mode) idx = @@colorset[mode].find_index(self) @@colorset[mode].at(idx+n).tap{|c| c.mode = mode } if idx end |
#prev(n = 1) ⇒ Object
Returns a previous color object in X11 colors. The color sequence is determined by its color mode.
112 113 114 |
# File 'lib/colorable/color.rb', line 112 def prev(n=1) self.next(-n) end |
#rgb ⇒ Object
75 76 77 |
# File 'lib/colorable/color.rb', line 75 def rgb _rgb.to_a end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/colorable/color.rb', line 31 def to_s @mode.to_s end |