Class: ZPNG::Color

Inherits:
Object
  • Object
show all
Includes:
DeepCopyable
Defined in:
lib/zpng/color.rb

Direct Known Subclasses

BMP::Color

Constant Summary collapse

BLACK =
Color.new(0  ,  0,  0)
WHITE =
Color.new(255,255,255)
RED =
Color.new(255,  0,  0)
GREEN =
Color.new(0  ,255,  0)
BLUE =
Color.new(0  ,  0,255)
YELLOW =
Color.new(255,255,  0)
CYAN =
Color.new(  0,255,255)
PURPLE =
MAGENTA =
Color.new(255,  0,255)
TRANSPARENT =
Color.new(0,0,0,0)
ANSI_COLORS =
[:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
ASCII_MAP =

see misc/gen_ascii_map.rb

["        '''''''```,,",
",,---:::::;;;;~~\"\"\"\"",
"\"!!!!!!<++*^^^(((LLJ",
"=??vvv]ts[j1122FFuoo",
"CeyyPEah55333VVmmXA4",
"G9$666666RRRRRR00MQQ",
"NNW####&&&&&%%%%%%%%",
"@@@@@@@"].join

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeepCopyable

#deep_copy

Constructor Details

#initialize(*a) ⇒ Color

Returns a new instance of Color.



9
10
11
12
13
14
15
16
17
18
# File 'lib/zpng/color.rb', line 9

def initialize *a
  h = a.last.is_a?(Hash) ? a.pop : {}
  @r,@g,@b,@a = *a

  # default sample depth for r,g,b and alpha = 8 bits
  @depth       = h[:depth]       || 8

  # default ALPHA = 0xff - opaque
  @a ||= h[:alpha] || h[:a] || (2**@depth-1)
end

Instance Attribute Details

#aObject Also known as: alpha

Returns the value of attribute a.



4
5
6
# File 'lib/zpng/color.rb', line 4

def a
  @a
end

#bObject

Returns the value of attribute b.



3
4
5
# File 'lib/zpng/color.rb', line 3

def b
  @b
end

#depthObject

Returns the value of attribute depth.



5
6
7
# File 'lib/zpng/color.rb', line 5

def depth
  @depth
end

#gObject

Returns the value of attribute g.



3
4
5
# File 'lib/zpng/color.rb', line 3

def g
  @g
end

#rObject

Returns the value of attribute r.



3
4
5
# File 'lib/zpng/color.rb', line 3

def r
  @r
end

Class Method Details

.from_grayscale(value, *args) ⇒ Object

from_grayscale level from_grayscale level, :depth => 16 from_grayscale level, alpha from_grayscale level, alpha, :depth => 16



96
97
98
# File 'lib/zpng/color.rb', line 96

def from_grayscale value, *args
  Color.new value,value,value, *args
end

.from_html(value, *args) ⇒ Object Also known as: from_css

value: (String) “#ff00ff”, “#f0f”, “f0f”, “eebbcc” alpha can be set via :alpha => N optional hash argument



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/zpng/color.rb', line 102

def from_html value, *args
  s = value.tr('#','')
  case s.size
  when 3
    r,g,b = s.split('').map{ |x| x.to_i(16)*17 }
  when 6
    r,g,b = s.scan(/../).map{ |x| x.to_i(16) }
  else
    raise ArgumentError, "invalid HTML color #{s}"
  end
  Color.new r,g,b, *args
end

Instance Method Details

#&(c) ⇒ Object

AND this color with other one, returns new Color



235
236
237
# File 'lib/zpng/color.rb', line 235

def & c
  op :&, c
end

#+(c) ⇒ Object

add other color to this one, returns new Color



225
226
227
# File 'lib/zpng/color.rb', line 225

def + c
  op :+, c
end

#-(c) ⇒ Object

subtract other color from this one, returns new Color



220
221
222
# File 'lib/zpng/color.rb', line 220

def - c
  op :-, c
end

#<=>(c) ⇒ Object

compare with other color



208
209
210
211
212
213
214
215
216
217
# File 'lib/zpng/color.rb', line 208

def <=> c
  c1,c2 =
    if self.depth > c.depth
      [self, c.to_depth(self.depth)]
    else
      [self.to_depth(c.depth), c]
    end
  r = c1.to_grayscale <=> c2.to_grayscale
  r == 0 ? (c1.to_a <=> c2.to_a) : r
end

#==(c) ⇒ Object Also known as: eql?

compare with other color



195
196
197
198
199
200
201
202
203
204
# File 'lib/zpng/color.rb', line 195

def == c
  return false unless c.is_a?(Color)
  c1,c2 =
    if self.depth > c.depth
      [self, c.to_depth(self.depth)]
    else
      [self.to_depth(c.depth), c]
    end
  c1.r == c2.r && c1.g == c2.g && c1.b == c2.b && c1.a == c2.a
end

#^(c) ⇒ Object

XOR this color with other one, returns new Color



230
231
232
# File 'lib/zpng/color.rb', line 230

def ^ c
  op :^, c
end

#black?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/zpng/color.rb', line 71

def black?
  r == 0 && g == 0 && b == 0
end

#euclidian(other_color) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/zpng/color.rb', line 58

def euclidian other_color
  # TODO: different depths
  r  = (self.r.to_i - other_color.r.to_i)**2
  r += (self.g.to_i - other_color.g.to_i)**2
  r += (self.b.to_i - other_color.b.to_i)**2
  Math.sqrt r
end

#hashObject

for Array.uniq()



267
268
269
# File 'lib/zpng/color.rb', line 267

def hash
  self.to_i
end

#inspectObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/zpng/color.rb', line 176

def inspect
  s = "#<ZPNG::Color"
  if depth == 16
    s << " r=" + (r ? "%04x" % r : "????")
    s << " g=" + (g ? "%04x" % g : "????")
    s << " b=" + (b ? "%04x" % b : "????")
    s << " alpha=%04x" % alpha if alpha && alpha != 0xffff
  else
    s << " #"
    s << (r ? "%02x" % r : "??")
    s << (g ? "%02x" % g : "??")
    s << (b ? "%02x" % b : "??")
    s << " alpha=%02x" % alpha if alpha && alpha != 0xff
  end
  s << " depth=#{depth}" if depth != 8
  s << ">"
end

#op(op, c = nil) ⇒ Object

Op! op! op! Op!! Oppan Gangnam Style!!



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/zpng/color.rb', line 245

def op op, c=nil
  # XXX what to do with alpha?
  max = 2**depth-1
  if c
    c = c.to_depth(depth)
    Color.new(
      @r.send(op, c.r) & max,
      @g.send(op, c.g) & max,
      @b.send(op, c.b) & max,
      :depth => self.depth
    )
  else
    Color.new(
      @r.send(op) & max,
      @g.send(op) & max,
      @b.send(op) & max,
      :depth => self.depth
    )
  end
end

#opaque?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/zpng/color.rb', line 79

def opaque?
  a.nil? || a == 2**depth-1
end

#to_aObject



128
129
130
# File 'lib/zpng/color.rb', line 128

def to_a
  [r, g, b, a]
end

#to_ansiObject

convert to ANSI color name



142
143
144
145
146
147
# File 'lib/zpng/color.rb', line 142

def to_ansi
  return to_depth(8).to_ansi if depth != 8
  a = ANSI_COLORS.map{|c| self.class.const_get(c.to_s.upcase) }
  a.map!{ |c| self.euclidian(c) }
  ANSI_COLORS[a.index(a.min)]
end

#to_ascii(map = ASCII_MAP) ⇒ Object

try to convert to one pseudographics ASCII character



136
137
138
139
# File 'lib/zpng/color.rb', line 136

def to_ascii map=ASCII_MAP
  #p self
  map[self.to_grayscale*(map.size-1)/(2**@depth-1), 1]
end

#to_cssObject Also known as: to_html

HTML/CSS color in notation like #33aa88



150
151
152
153
# File 'lib/zpng/color.rb', line 150

def to_css
  return to_depth(8).to_css if depth != 8
  "#%02X%02X%02X" % [r,g,b]
end

#to_depth(new_depth) ⇒ Object

change bit depth, return new Color



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/zpng/color.rb', line 159

def to_depth new_depth
  return self if depth == new_depth

  color = Color.new :depth => new_depth
  if new_depth > self.depth
    %w'r g b a'.each do |part|
      color.send("#{part}=", (2**new_depth-1)/(2**depth-1)*self.send(part))
    end
  else
    # new_depth < self.depth
    %w'r g b a'.each do |part|
      color.send("#{part}=", self.send(part)>>(self.depth-new_depth))
    end
  end
  color
end

#to_gray_alphaObject



87
88
89
# File 'lib/zpng/color.rb', line 87

def to_gray_alpha
  [to_grayscale, alpha]
end

#to_grayscaleObject



83
84
85
# File 'lib/zpng/color.rb', line 83

def to_grayscale
  (r+g+b)/3
end

#to_iObject

simple conversions



120
121
122
# File 'lib/zpng/color.rb', line 120

def to_i
  ((a||0) << 24) + ((r||0) << 16) + ((g||0) << 8) + (b||0)
end

#to_sObject



124
125
126
# File 'lib/zpng/color.rb', line 124

def to_s
  "%02X%02X%02X" % [r,g,b]
end

#transparent?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/zpng/color.rb', line 75

def transparent?
  a == 0
end

#white?Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/zpng/color.rb', line 66

def white?
  max = 2**depth-1
  r == max && g == max && b == max
end

#|(c) ⇒ Object

OR this color with other one, returns new Color



240
241
242
# File 'lib/zpng/color.rb', line 240

def | c
  op :|, c
end