Class: Hermeneutics::Color

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

Overview

Generate HTML color values.

Examples

red = Color.new 0xff, 0, 0
black = Color.grey 0
red.to_s                           #=> "#ff0000"
red.to_fract                       #=> [ 1.0, 0.0, 0.0]

(Color.new 0xff, 0, 0).to_hsv      #=> [0.0, 1.0, 1.0]
(Color.new 0, 0xff, 0).to_hsv      #=> [120.0, 1.0, 1.0]
(Color.new 0, 0x7f, 0x7f).to_hsv   #=> [180.0, 1.0, 0.49804]
(Color.new 0, 0x80, 0x80).to_hsv   #=> [180.0, 1.0, 0.50196]
(Color.from_hsv 180, 1, 0.5).to_s  #=> "#007f7f"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bObject

Returns the value of attribute b.



36
37
38
# File 'lib/hermeneutics/color.rb', line 36

def b
  @b
end

#gObject

Returns the value of attribute g.



36
37
38
# File 'lib/hermeneutics/color.rb', line 36

def g
  @g
end

#rObject

Returns the value of attribute r.



36
37
38
# File 'lib/hermeneutics/color.rb', line 36

def r
  @r
end

Class Method Details

.from_fract(rf, gf, bf) ⇒ Object

:call-seq:

from_fract( rf, gf, bf)    -> clr

Build a Color from three values in range 0..1 where 1.0 means 255.



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

def from_fract rf, gf, bf
  rgb = [rf, gf, bf].map { |x| 0xff * x }
  new *rgb
end

.from_hsv(h, s, v) ⇒ Object

:call-seq:

from_hsv( h, s, v)    -> clr

Build a Color from HSV parameters.

Ranges are:

h    0...360
s    0.0..1.0
v    0.0..1.0


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/hermeneutics/color.rb', line 201

def from_hsv h, s, v
  if s.nonzero? then
    h /= 60.0
    i = h.to_i % 6
    f = h - i
    rgb = []
    if (i%2).zero? then
      rgb.push v
      rgb.push v * (1.0 - s * (1.0 - f))
    else
      rgb.push v * (1.0 - s * f)
      rgb.push v
    end
    rgb.push v * (1.0 - s)
    rgb.rotate! -(i/2)
    from_fract *rgb
  else
    from_fract v, v, v
  end
end

.from_long(rl, gl, bl) ⇒ Object

:call-seq:

from_fract( rl, gl, bl)    -> clr

Build a Color from three values in range 0..0xffff.



156
157
158
159
# File 'lib/hermeneutics/color.rb', line 156

def from_long rl, gl, bl
  rgb = [rl, gl, bl].map { |x| x / 0x100 }
  new *rgb
end

.from_s(str) ⇒ Object

:call-seq:

from_s( str)    -> clr

Build a Color from an HTML tags key.



108
109
110
111
# File 'lib/hermeneutics/color.rb', line 108

def from_s str
  rgb = str.scan( /[0-9a-f]{2}/i).map do |x| x.to_i 0x10 end
  new *rgb
end

.gray(i) ⇒ Object Also known as: grey

:call-seq:

gray( num)           -> clr

Create a gray color (r=b=g). num is in range 0..255.



85
86
87
# File 'lib/hermeneutics/color.rb', line 85

def gray i
  new i, i, i
end

Instance Method Details

#==(oth) ⇒ Object



70
71
72
73
74
75
# File 'lib/hermeneutics/color.rb', line 70

def == oth
  case oth
    when Color then to_a == oth.to_a
    when Array then to_a == oth
  end
end

#complementaryObject

:call-seq:

complementary()                -> clr

Build the complementary color.



240
# File 'lib/hermeneutics/color.rb', line 240

def complementary ; Color.new *(tuple.map { |x| 0xff - x }) ; end

#edit_hsvObject

:call-seq:

edit_hsv() { |h,s,v| ... }    -> clr

Convert it to an HSV triple, yield that to the block and build a new Color from the blocks result.



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

def edit_hsv
  hsv = yield *to_hsv
  self.class.from_hsv *hsv
end

#inspectObject



99
# File 'lib/hermeneutics/color.rb', line 99

def inspect ; "#<#{self.class}:#{'0x%08x' % (object_id << 1)} #{to_s}>" ; end

#to_aObject Also known as: tuple

:call-seq:

to_a()    -> ary

Return RGB values as an array.



67
# File 'lib/hermeneutics/color.rb', line 67

def to_a ; [ @r, @g, @b] ; end

#to_fractObject

:call-seq:

to_fract()    -> ary

Return three values in range 0..1 where 1.0 means 255.



121
122
123
# File 'lib/hermeneutics/color.rb', line 121

def to_fract
  tuple.map { |x| x / 255.0 }
end

#to_hsvObject

:call-seq:

to_hsv()    -> ary

Convert it to an HSV triple.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/hermeneutics/color.rb', line 169

def to_hsv
  rgb = [ @r, @g, @b].map { |x| (Integer x) / 255.0 }
  v = rgb.max
  delta = v - rgb.min
  unless delta > 0.0 then
    h = s = 0.0
  else
    s = delta / v
    r, g, b = rgb
    case v
      when r then h =     (g - b) / delta ; h += 6 if h < 0
      when g then h = 2 + (b - r) / delta
      when b then h = 4 + (r - g) / delta
    end
    h *= 60
  end
  [ h, s, v]
end

#to_longObject

:call-seq:

to_long()    -> ary

Extend it to a 48-bit color triple.



145
146
147
# File 'lib/hermeneutics/color.rb', line 145

def to_long
  tuple.map { |x| x * 0x100 + x }
end

#to_sObject

:call-seq:

to_s()    -> str

Return color as an HTML tags key.



97
# File 'lib/hermeneutics/color.rb', line 97

def to_s ; "#" + tuple.map { |x| "%02x" % x }.join ; end