Class: Colorable::Color
- Inherits:
-
Object
- Object
- Colorable::Color
- Includes:
- Comparable
- Defined in:
- lib/colorable/color.rb
Defined Under Namespace
Classes: NameError
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.
15 16 17 |
# File 'lib/colorable/color.rb', line 15 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.
153 154 155 |
# File 'lib/colorable/color.rb', line 153 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 Fixnum: addiction applies based on its color mode.
126 127 128 129 130 131 132 133 |
# File 'lib/colorable/color.rb', line 126 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 Fixnum: subtruction applies based on its color mode.
140 141 142 143 144 145 146 147 |
# File 'lib/colorable/color.rb', line 140 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.
161 162 163 |
# File 'lib/colorable/color.rb', line 161 def /(other) new_by_composed_rgb(:/, other) end |
#<=>(other) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/colorable/color.rb', line 93 def <=>(other) if [self.name, other.name].any?(&:empty?) self.rgb <=> other.rgb else self.name <=> other.name end end |
#_hex ⇒ Object
60 61 62 |
# File 'lib/colorable/color.rb', line 60 def _hex @hex ||= HEX.new _rgb.to_hex end |
#_hsb ⇒ Object
64 65 66 |
# File 'lib/colorable/color.rb', line 64 def _hsb @hsb ||= HSB.new *_rgb.to_hsb end |
#_name ⇒ Object
52 53 54 |
# File 'lib/colorable/color.rb', line 52 def _name @name end |
#_rgb ⇒ Object
56 57 58 |
# File 'lib/colorable/color.rb', line 56 def _rgb @rgb end |
#dark? ⇒ Boolean
117 118 119 |
# File 'lib/colorable/color.rb', line 117 def dark? !!DARK_COLORS.detect { |d| d == self.name } end |
#hex ⇒ Object
72 73 74 |
# File 'lib/colorable/color.rb', line 72 def hex _hex.to_s end |
#hsb ⇒ Object Also known as: hsv
80 81 82 |
# File 'lib/colorable/color.rb', line 80 def hsb _hsb.to_a end |
#info ⇒ Object
Returns information of the color object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/colorable/color.rb', line 41 def info { name: name, rgb: rgb, hsb: hsb, hex: hex, mode: mode, dark: dark? } end |
#inspect ⇒ Object
36 37 38 |
# File 'lib/colorable/color.rb', line 36 def inspect "#<%s '%s<%s/%s/%s>'>" % [self.class, _name, _rgb, _hsb, _hex] end |
#mode ⇒ Object
Returns a current output mode
20 21 22 |
# File 'lib/colorable/color.rb', line 20 def mode "#{@mode.class}"[/\w+$/].intern end |
#mode=(mode) ⇒ Object
Set output mode.
25 26 27 28 29 30 |
# File 'lib/colorable/color.rb', line 25 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
68 69 70 |
# File 'lib/colorable/color.rb', line 68 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.
104 105 106 107 108 |
# File 'lib/colorable/color.rb', line 104 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.
113 114 115 |
# File 'lib/colorable/color.rb', line 113 def prev(n=1) self.next(-n) end |
#rgb ⇒ Object
76 77 78 |
# File 'lib/colorable/color.rb', line 76 def rgb _rgb.to_a end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/colorable/color.rb', line 32 def to_s @mode.to_s end |