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 Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rgb ⇒ Object
readonly
Returns the value of attribute rgb.
Instance Method Summary collapse
-
#+(arg) ⇒ Object
Returns a color object which has incremented color values.
-
#-(arg) ⇒ Object
Returns a color object which has decremented color values.
- #<=>(other) ⇒ 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.
-
#mode ⇒ Object
Returns a current output mode.
-
#mode=(mode) ⇒ Object
Set output mode.
-
#next(n = 1) ⇒ Object
Returns a next color object in X11 colors.
-
#prev(n = 1) ⇒ Object
Returns a previous color object in X11 colors.
- #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.
16 17 18 |
# File 'lib/colorable/color.rb', line 16 def initialize(arg) @name, @rgb, @hsb, @hex, @mode = set_variables(arg) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/colorable/color.rb', line 5 def name @name end |
#rgb ⇒ Object (readonly)
Returns the value of attribute rgb.
5 6 7 |
# File 'lib/colorable/color.rb', line 5 def rgb @rgb end |
Instance Method Details
#+(arg) ⇒ Object
Returns a color object which has incremented color values. Array of values or a Fixnum is acceptable as an argument. Which values are affected is determined by its color mode.
92 93 94 |
# File 'lib/colorable/color.rb', line 92 def +(arg) self.class.new @mode + arg end |
#-(arg) ⇒ Object
Returns a color object which has decremented color values. Array of values or a Fixnum is acceptable as an argument. Which values are affected is determined by its color mode.
99 100 101 |
# File 'lib/colorable/color.rb', line 99 def -(arg) self.class.new @mode - arg end |
#<=>(other) ⇒ Object
66 67 68 |
# File 'lib/colorable/color.rb', line 66 def <=>(other) self.rgb <=> other.rgb end |
#dark? ⇒ Boolean
85 86 87 |
# File 'lib/colorable/color.rb', line 85 def dark? !!DARK_COLORS.detect { |d| d == name.to_s } end |
#hex ⇒ Object
49 50 51 |
# File 'lib/colorable/color.rb', line 49 def hex @hex ||= HEX.new rgb.to_hex end |
#hsb ⇒ Object Also known as: hsv
53 54 55 |
# File 'lib/colorable/color.rb', line 53 def hsb @hsb ||= HSB.new *rgb.to_hsb end |
#info ⇒ Object
Returns information of the color object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/colorable/color.rb', line 38 def info { NAME: name.to_s, RGB: rgb.to_a, HSB: hsb.to_a, HEX: hex.to_s, MODE: mode, DARK: dark? } end |
#mode ⇒ Object
Returns a current output mode
21 22 23 |
# File 'lib/colorable/color.rb', line 21 def mode "#{@mode.class}"[/\w+$/].intern end |
#mode=(mode) ⇒ Object
Set output mode.
26 27 28 29 30 31 |
# File 'lib/colorable/color.rb', line 26 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 |
#next(n = 1) ⇒ Object
Returns a next color object in X11 colors. The color sequence is determined by its color mode.
73 74 75 76 77 |
# File 'lib/colorable/color.rb', line 73 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.
81 82 83 |
# File 'lib/colorable/color.rb', line 81 def prev(n=1) self.next(-n) end |
#to_s ⇒ Object
33 34 35 |
# File 'lib/colorable/color.rb', line 33 def to_s @mode.to_s end |