Class: Tgui::Color

Inherits:
ExternObject show all
Defined in:
lib/white_gold/dsl/color.rb,
lib/white_gold/generated/tgui-abi-loader.gf.rb

Constant Summary collapse

PREDEFINED_COLORS =
{
  red: [200, 0, 0, 255],
  green: [0, 200, 0, 255],
  blue: [0, 0, 200, 255],
  yellow: [200, 200, 0, 255],
  white: [200, 200, 200, 255],
  black: [0, 0, 0, 255]
}

Instance Attribute Summary

Attributes inherited from ExternObject

#pointer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ExternObject

abi_attr, abi_bit_enum, abi_def, abi_enum, #abi_pack, abi_signal, abi_static, #abi_unpack, callback_storage, callback_storage=, data_storage=, finalizer, global_callback_storage, global_callback_storage=, #initialized, self_abi_def, self_abi_def_setter

Methods included from Packer

#abi_pack, #abi_packer, #abi_packer_method_name

Methods included from Unpacker

#abi_unpack, #abi_unpacker, #abi_unpacker_method_name

Methods included from BangDef

#def!

Constructor Details

#initialize(*a, pointer: nil) ⇒ Color

Returns a new instance of Color.



805
# File 'lib/white_gold/generated/tgui-abi-loader.gf.rb', line 805

def initialize(*a, pointer: nil);    Abi.call_arg_map! a; super(pointer: pointer || Abi.ABI_Color_new(*a)); initialized(); end

Class Method Details

.from(*arg) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/white_gold/dsl/color.rb', line 15

def self.from *arg
  case arg.size
  when 1
    arg = arg.first
    case arg
    when Color
      return arg
    when String
      r, g, b, a = *tones_from_string(arg)
    when Symbol
      r, g, b, a = *PREDEFINED_COLORS[arg]
    when Numeric
      r = g = b = arg
      a = 255
    else raise "Unsupported argument #{arg} #{arg.class}"
    end
  when 2
    r = g = b = arg[0]
    a = arg[1]
  when 3
    r, g, b = *arg
    a = 255
  when 4
    r, g, b, a = *arg
  else raise "Unsupported argument #{arg}"
  end
  Color.new r, g, b, a
end

.hsv_to_rgb(hue, saturation, value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/white_gold/dsl/color.rb', line 98

def self.hsv_to_rgb(hue, saturation, value)
  return [value, value, value] if saturation == 0

  region = hue / 43;
  remainder = (hue - (region * 43.0)) * 6; 
  
  x = (255.0 - saturation) * value / 256.0;
  y = (255.0 - ((saturation * remainder) / 256.0)) * value / 256.0;
  z = (255.0 - ((saturation * (255.0 - remainder)) / 256.0)) * value / 256.0;
  
  return case region
  when 0
    [value, z, x]
  when 1
    [y, value, x]
  when 2
    [x, value, z]
  when 3
    [x, y, value]
  when 4
    [z, x, value]
  else
    [value, x, y]
  end.map(&:round)
end

.rgb_to_hsv(red, green, blue) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/white_gold/dsl/color.rb', line 80

def self.rgb_to_hsv(red, green, blue)
  min, max = *[red, green, blue].minmax
  mdiff = max - min
  if min == max
    hue = 0.0
  elsif red == max
    hue = (green - blue) * 43.0 / mdiff
  elsif green == max
    hue = 85 + (blue - red) * 43.0 / mdiff
  else
    hue = 171 + (red - green) * 43.0 / mdiff
  end

  hue += 256 if hue < 0
  saturation = max > 0 ? mdiff * 255.0 / max : 0.0
  [hue, saturation, max].map(&:round)
end

.tones_from_string(str) ⇒ Object



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

def self.tones_from_string str
  str.delete_prefix("#").scan(/../).map(&:hex) + Array.new(4, 255)
end

Instance Method Details

#_abi_apply_opacity(*a) ⇒ Object



810
# File 'lib/white_gold/generated/tgui-abi-loader.gf.rb', line 810

def _abi_apply_opacity(*a);    Abi.call_arg_map! a; Abi.ABI_Color_applyOpacity(@pointer, *a); end

#_abi_get_alpha(*a) ⇒ Object



809
# File 'lib/white_gold/generated/tgui-abi-loader.gf.rb', line 809

def _abi_get_alpha(*a);    Abi.call_arg_map! a; Abi.ABI_Color_get_alpha(@pointer, *a); end

#_abi_get_blue(*a) ⇒ Object



808
# File 'lib/white_gold/generated/tgui-abi-loader.gf.rb', line 808

def _abi_get_blue(*a);    Abi.call_arg_map! a; Abi.ABI_Color_get_blue(@pointer, *a); end

#_abi_get_green(*a) ⇒ Object



807
# File 'lib/white_gold/generated/tgui-abi-loader.gf.rb', line 807

def _abi_get_green(*a);    Abi.call_arg_map! a; Abi.ABI_Color_get_green(@pointer, *a); end

#_abi_get_red(*a) ⇒ Object



806
# File 'lib/white_gold/generated/tgui-abi-loader.gf.rb', line 806

def _abi_get_red(*a);    Abi.call_arg_map! a; Abi.ABI_Color_get_red(@pointer, *a); end

#brightnessObject



50
51
52
# File 'lib/white_gold/dsl/color.rb', line 50

def brightness
  Color.rgb_to_hsv(red, green, blue)[2]
end

#darker(shadow) ⇒ Object



59
60
61
62
# File 'lib/white_gold/dsl/color.rb', line 59

def darker shadow
  h, s, v = *Color.rgb_to_hsv(red, green, blue)
  Color.new *Color.hsv_to_rgb(h, s, (v - shadow).clamp(0, 255)), alpha
end

#inspectObject



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

def inspect
  to_s
end

#lighter(light) ⇒ Object



54
55
56
57
# File 'lib/white_gold/dsl/color.rb', line 54

def lighter light
  h, s, v = *Color.rgb_to_hsv(red, green, blue)
  Color.new *Color.hsv_to_rgb(h, s, (v + light).clamp(0, 255)), alpha
end

#to_aObject



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

def to_a
  [red, green, blue, alpha]
end

#to_sObject



68
69
70
# File 'lib/white_gold/dsl/color.rb', line 68

def to_s
  "##{to_a.map{ _1.to_s(16).ljust(2, '0') }.join}"
end