Class: UnderOs::Color

Inherits:
Object show all
Defined in:
lib/under_os/color.rb

Defined Under Namespace

Modules: Converter

Constant Summary collapse

CACHE =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui_color) ⇒ Color

Returns a new instance of Color.



11
12
13
# File 'lib/under_os/color.rb', line 11

def initialize(ui_color)
  @ui = ui_color
end

Instance Attribute Details

#uiObject

Returns the value of attribute ui.



2
3
4
# File 'lib/under_os/color.rb', line 2

def ui
  @ui
end

Class Method Details

.new(*args) ⇒ Object



6
7
8
9
# File 'lib/under_os/color.rb', line 6

def self.new(*args)
  color = Converter.ui_color_from(*args)
  CACHE[color] ||= super(color)
end

Instance Method Details

#==(color) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/under_os/color.rb', line 60

def ==(color)
  color = if color.is_a?(self.class)
    color
  else
    color = [color] if ! color.is_a?(Array)
    self.class.new(*color)
  end

  to_s == color.to_s
end

#cgObject



15
16
17
# File 'lib/under_os/color.rb', line 15

def cg
  @ui.CGColor
end

#ciObject



19
20
21
# File 'lib/under_os/color.rb', line 19

def ci
  @ui.CIColor
end

#invertObject



23
24
25
26
27
28
29
# File 'lib/under_os/color.rb', line 23

def invert
  self.class.new to_rgba.tap do |rgba|
    rgba[0] = 255 - rgba[0]
    rgba[1] = 255 - rgba[1]
    rgba[2] = 255 - rgba[2]
  end
end

#to_hexObject



50
51
52
53
54
# File 'lib/under_os/color.rb', line 50

def to_hex
  r, g, b = to_rgb
  int = (r << 16) + (g << 8) + b
  '#' + int.to_s(16).rjust(6, '0')
end

#to_rgbObject



46
47
48
# File 'lib/under_os/color.rb', line 46

def to_rgb
  to_rgba.slice(0, 3)
end

#to_rgbaObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/under_os/color.rb', line 31

def to_rgba
  r = Pointer.new(:float)
  g = Pointer.new(:float)
  b = Pointer.new(:float)
  a = Pointer.new(:float)

  @ui.getRed(r, green:g, blue:b, alpha:a)

  r = (255 * r[0]).round
  g = (255 * g[0]).round
  b = (255 * b[0]).round

  [r,g,b,a[0]]
end

#to_sObject



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

def to_s
  to_hex
end