Class: Mittsu::Color

Inherits:
Vector3 show all
Defined in:
lib/mittsu/math/color.rb

Constant Summary collapse

ELEMENTS =
{ r: 0, g: 1, b: 2 }
DIMENSIONS =
ELEMENTS.count

Instance Attribute Summary

Attributes inherited from Vector

#elements, #index, #uv

Instance Method Summary collapse

Methods inherited from Vector3

#add_scaled_vector, #apply_axis_angle, #apply_euler, #apply_matrix3, #apply_matrix4, #apply_projection, #apply_quaternion, #clamp_length, #cross, #cross_vectors, #distance_to_squared, #dot, #equals, #from_attribute, #from_buffer_attribute, #get_component, #length_manhattan, #manhattan_distance_to, #project, #random, #random_direction, #set_component, #set_from_cylindrical, #set_from_cylindrical_coords, #set_from_matrix3_column, #set_from_matrix_column, #set_from_matrix_position, #set_from_matrix_scale, #set_from_spherical, #set_from_spherical_coords, #set_scalar, #transform_direction, #unproject, #x, #x=, #y, #y=, #z, #z=

Methods inherited from Vector

#==, #[], #[]=, #add, #add_scalar, #add_vectors, #angle_to, #ceil, #clamp, #clamp_scalar, #clone, #copy, #distance_to, #divide, #divide_scalar, #each_dimension, #floor, #from_array, #length, #length_sq, #lerp, #lerp_vectors, #max, #min, #multiply, #multiply_scalar, #multiply_vectors, #negate, #normalize, #project_on_plane, #project_on_vector, #reflect, #round, #round_to_zero, #set_length, #sub, #sub_scalar, #sub_vectors, #to_array, #to_s

Constructor Details

#initialize(*args) ⇒ Color

Returns a new instance of Color.



8
9
10
11
12
13
14
15
16
# File 'lib/mittsu/math/color.rb', line 8

def initialize(*args)
  super(0, 0, 0)
  case args.length
  when 3 then self.set_rgb(*args)
  when 1 then self.set(args.first)
  when 0 then self.set_rgb(1.0, 1.0, 1.0)
  else raise ArgumentError, "Arguments must be (r, g, b), (color), or none"
  end
end

Instance Method Details

#convert_gamma_to_linearObject



114
115
116
117
118
119
120
# File 'lib/mittsu/math/color.rb', line 114

def convert_gamma_to_linear
  rr, gg, bb = self.r, self.g, self.b
  self.r = rr * rr
  self.g = gg * gg
  self.b = bb * bb
  self
end

#convert_linear_to_gammaObject



122
123
124
125
126
127
# File 'lib/mittsu/math/color.rb', line 122

def convert_linear_to_gamma
  self.r = ::Math.sqrt(self.r)
  self.g = ::Math.sqrt(self.g)
  self.b = ::Math.sqrt(self.b)
  self
end

#copy_gamma_to_linear(color, gamma_factor = 2.0) ⇒ Object



99
100
101
102
103
104
# File 'lib/mittsu/math/color.rb', line 99

def copy_gamma_to_linear(color, gamma_factor = 2.0)
  self.r = color.r ** gamma_factor
  self.g = color.g ** gamma_factor
  self.b = color.b ** gamma_factor
  self
end

#copy_linear_to_gamma(color, gamma_factor = 2.0) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/mittsu/math/color.rb', line 106

def copy_linear_to_gamma(color, gamma_factor = 2.0)
  safe_inverse = (gamma_factor > 0) ? (1.0 / gamma_factor) : 1.0
  self.r = color.r ** safe_inverse
  self.g = color.g ** safe_inverse
  self.b = color.b ** safe_inverse
  self
end

#hexObject



129
130
131
# File 'lib/mittsu/math/color.rb', line 129

def hex
  (self.r * 255).to_i << 16 ^ (self.g * 255).to_i << 8 ^ (self.b * 255).to_i << 0
end

#hex_stringObject



133
134
135
# File 'lib/mittsu/math/color.rb', line 133

def hex_string
  ('000000' + self.hex.to_s(16))[-6..-1]
end

#hsl(target = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mittsu/math/color.rb', line 137

def hsl(target = nil)
  # h,s,l ranges are in 0.0 - 1.0
  hsl = target || { h: 0.0, s: 0.0, l: 0.0 }
  rr, gg, bb = self.r, self.g, self.b
  max = [r, g, b].max
  min = [r, g, b].min
  hue, saturation = nil, nil
  lightness = (min + max) / 2.0
  if min == max
    hue = 0.0
    saturation = 0.0
  else
    delta = max - min
    saturation = lightness <= 0.5 ? delta / (max + min) : delta / (2.0 - max - min)
    case max
    when rr then hue = (gg - bb) / delta + (gg < bb ? 6.0 : 0.0)
    when gg then hue = (bb - rr) / delta + 2.0
    when bb then hue = (rr - gg) / delta + 4.0
    end
    hue /= 6.0
  end
  hsl[:h] = hue
  hsl[:s] = saturation
  hsl[:l] = lightness
  hsl
end

#offset_hsl(h, s, l) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/mittsu/math/color.rb', line 168

def offset_hsl(h, s, l)
  hsl = self.hsl
  hsl[:h] += h
  hsl[:s] += s
  hsl[:l] += l
  self.set_hsl(hsl[:h], hsl[:s], hsl[:l])
  self
end

#set(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mittsu/math/color.rb', line 26

def set(value)
  case value
  when Color
    self.copy(value)
  when Integer
    self.set_hex(value)
  when String
    self.set_style(value)
  else
    raise ArgumentError, "Arguments must be Color, Integer or String"
  end
  self
end

#set_hex(hex) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/mittsu/math/color.rb', line 40

def set_hex(hex)
  hex = hex.floor
  self.r = (hex >> 16 & 255) / 255.0
  self.g = (hex >> 8 & 255) / 255.0
  self.b = (hex & 255) / 255.0
  self
end

#set_hsl(h, s, l) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mittsu/math/color.rb', line 53

def set_hsl(h, s, l)
  # h,s,l ranges are in 0.0 - 1.0
  if s.zero?
    self.r = self.g = self.b = l
  else
    p = l <= 0.5 ? l * (1.0 + s) : l + s - (l * s)
    q = (2.0 * l) - p
    self.r = hue2rgb(q, p, h + 1.0 / 3.0)
    self.g = hue2rgb(q, p, h)
    self.b = hue2rgb(q, p, h - 1.0 / 3.0)
  end
  self
end

#set_rgb(r, g, b) ⇒ Object



48
49
50
51
# File 'lib/mittsu/math/color.rb', line 48

def set_rgb(r, g, b)
  @elements = [r.to_f, g.to_f, b.to_f]
  self
end

#set_style(style) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mittsu/math/color.rb', line 67

def set_style(style)
  # rgb(255,0,0)
  if /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i =~ style
    self.r = [255.0, $1.to_f].min / 255.0
    self.g = [255.0, $2.to_f].min / 255.0
    self.b = [255.0, $3.to_f].min / 255.0
    return self
  end
  # rgb(100%,0%,0%)
  if /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i =~ style
    self.r = [100.0, $1.to_f].min / 100.0
    self.g = [100.0, $2.to_f].min / 100.0
    self.b = [100.0, $3.to_f].min / 100.0
    return self
  end
  # #ff0000
  if /^\#([0-9a-f]{6})$/i =~ style
    self.set_hex($1.hex)
    return self
  end
  # #f00
  if /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i =~ style
    self.set_hex(($1 + $1 + $2 + $2 + $3 + $3).hex)
    return self
  end
  # red
  if /^(\w+)$/i =~ style
    self.set_hex(Mittsu::ColorKeywords[style])
    return self
  end
end

#styleObject



164
165
166
# File 'lib/mittsu/math/color.rb', line 164

def style
  "rgb(#{ (self.r * 255).to_i },#{ (self.g * 255).to_i },#{ (self.b * 255).to_i })"
end