Class: Colorable::Color

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/colorable/color.rb

Defined Under Namespace

Classes: NameError

Constant Summary collapse

@@colorset =
{}

Instance Method Summary collapse

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

#_hexObject



60
61
62
# File 'lib/colorable/color.rb', line 60

def _hex
  @hex ||= HEX.new _rgb.to_hex
end

#_hsbObject



64
65
66
# File 'lib/colorable/color.rb', line 64

def _hsb
  @hsb ||= HSB.new *_rgb.to_hsb
end

#_nameObject



52
53
54
# File 'lib/colorable/color.rb', line 52

def _name
  @name
end

#_rgbObject



56
57
58
# File 'lib/colorable/color.rb', line 56

def _rgb
  @rgb
end

#dark?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/colorable/color.rb', line 117

def dark?
  !!DARK_COLORS.detect { |d| d == self.name }
end

#hexObject



72
73
74
# File 'lib/colorable/color.rb', line 72

def hex
  _hex.to_s
end

#hsbObject Also known as: hsv



80
81
82
# File 'lib/colorable/color.rb', line 80

def hsb
  _hsb.to_a
end

#infoObject

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

#inspectObject



36
37
38
# File 'lib/colorable/color.rb', line 36

def inspect
  "#<%s '%s<%s/%s/%s>'>" % [self.class, _name, _rgb, _hsb, _hex]
end

#modeObject

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

#nameObject



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

#rgbObject



76
77
78
# File 'lib/colorable/color.rb', line 76

def rgb
  _rgb.to_a
end

#to_sObject



32
33
34
# File 'lib/colorable/color.rb', line 32

def to_s
  @mode.to_s
end