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] || (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



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

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

Instance Method Details

#<=>(c) ⇒ Object

compare with other color



190
191
192
193
194
195
196
197
198
199
# File 'lib/zpng/color.rb', line 190

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



177
178
179
180
181
182
183
184
185
186
# File 'lib/zpng/color.rb', line 177

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

#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()



202
203
204
# File 'lib/zpng/color.rb', line 202

def hash
  self.to_i
end

#inspectObject



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

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

#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



110
111
112
# File 'lib/zpng/color.rb', line 110

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

#to_ansiObject

convert to ANSI color name



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

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



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

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



132
133
134
135
# File 'lib/zpng/color.rb', line 132

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/zpng/color.rb', line 141

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



102
103
104
# File 'lib/zpng/color.rb', line 102

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

#to_sObject



106
107
108
# File 'lib/zpng/color.rb', line 106

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