class Hermes::Colour
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"
Attributes
b[R]
g[R]
r[R]
Public Class Methods
new( r, g, b) → clr
click to toggle source
Create a color with red, green and blue values. They are in range
0..255
.
# File lib/hermes/color.rb, line 46 def initialize r, *gb if gb.any? then @r, @g, @b = *[ r, *gb].map { |x| ff x } else @r = @g = @b = (ff r) end end
Public Instance Methods
==(oth)
click to toggle source
# File lib/hermes/color.rb, line 70 def == oth case oth when Color then to_a == oth.to_a when Array then to_a == oth end end
b=(x ;)
click to toggle source
# File lib/hermes/color.rb, line 60 def b= x ; @b = ff x ; end
complemetary() → clr
click to toggle source
Build the complemetary color.
# File lib/hermes/color.rb, line 240 def complemetary ; Color.new *(tuple.map { |x| 0xff - x }) ; end
edit_hsv() { |h,s,v| ... } → clr
click to toggle source
Convert it to an HSV triple, yield that to the block and build a new
Color
from the blocks result.
# File lib/hermes/color.rb, line 230 def edit_hsv hsv = yield *to_hsv self.class.from_hsv *hsv end
from_fract( rf, gf, bf) → clr
click to toggle source
Build a Color from three values in range 0..1 where 1.0 means 255.
# File lib/hermes/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) → clr
click to toggle source
Build a Color from HSV parameters.
Ranges are:
h 0...360 s 0.0..1.0 v 0.0..1.0
# File lib/hermes/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_fract( rl, gl, bl) → clr
click to toggle source
Build a Color from three values in range 0..0xffff.
# File lib/hermes/color.rb, line 156 def from_long rl, gl, bl rgb = [rl, gl, bl].map { |x| x / 0x100 } new *rgb end
from_s( str) → clr
click to toggle source
Build a Color from an HTML tags key.
# File lib/hermes/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
g=(x ;)
click to toggle source
# File lib/hermes/color.rb, line 59 def g= x ; @g = ff x ; end
gray( num) → clr
click to toggle source
Create a gray color (r=b=g). num
is in range
0..255
.
# File lib/hermes/color.rb, line 85 def gray i new i, i, i end
inspect()
click to toggle source
# File lib/hermes/color.rb, line 99 def inspect ; "#<#{cls}:#{'0x%08x' % (object_id << 1)} #{to_s}>" ; end
r=(x ;)
click to toggle source
# File lib/hermes/color.rb, line 58 def r= x ; @r = ff x ; end
to_a() → ary
click to toggle source
Return RGB values as an array.
# File lib/hermes/color.rb, line 67 def to_a ; [ @r, @g, @b] ; end
Also aliased as: tuple
to_fract() → ary
click to toggle source
Return three values in range 0..1 where 1.0 means 255.
# File lib/hermes/color.rb, line 121 def to_fract tuple.map { |x| x / 255.0 } end
to_hsv() → ary
click to toggle source
Convert it to an HSV triple.
# File lib/hermes/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_long() → ary
click to toggle source
Extend it to a 48-bit color triple.
# File lib/hermes/color.rb, line 145 def to_long tuple.map { |x| x * 0x100 + x } end
to_s() → str
click to toggle source
Return color as an HTML tags key.
# File lib/hermes/color.rb, line 97 def to_s ; "#" + tuple.map { |x| "%02x" % x }.join ; end
Private Instance Methods
ff(x ;)
click to toggle source
# File lib/hermes/color.rb, line 54 def ff x ; (Integer x) & 0xff ; end