class GraphViz::Utils::Colors

Constants

COLORS
HEX_FOR_COLOR
RGBA

Attributes

a[R]
b[R]
g[R]
h[R]
r[R]
s[R]
v[R]

Public Class Methods

hsv(h, s, v) click to toggle source
# File lib/graphviz/utils/colors.rb, line 116
def hsv(h, s, v)
   color = Colors.new
   color.hsv(h, s, v)
   color
end
hsv_to_rgb(h, s, v) click to toggle source
# File lib/graphviz/utils/colors.rb, line 162
def hsv_to_rgb(h, s, v)
   _h, _s, _v = h.to_f * 360.0, s.to_f, v.to_f

   if _s == 0.0
     r = (_v * 255 ).to_i.to_s.convert_base(10,16)
     return [r, r, r]
   end

   _h = _h / 60.0
   i = _h.floor
   f = _h - i
   p = _v * ( 1.0 - _s )
   q = _v * ( 1.0 - _s * f )
   t = _v * ( 1.0 - _s * ( 1 - f ) )
   case i
   when 0
      r = _v
      g = t
      b = p
   when 1
      r = q
      g = _v
      b = p
   when 2
      r = p
      g = _v
      b = t
   when 3
      r = p
      g = q
      b = _v
   when 4
      r = t
      g = p
      b = _v
   else
      r = _v
      g = p
      b = q
   end

   [
      (r * 255).to_i.to_s.convert_base(10, 16),
      (g * 255).to_i.to_s.convert_base(10, 16),
      (b * 255).to_i.to_s.convert_base(10, 16)
   ]
end
name(c) click to toggle source
# File lib/graphviz/utils/colors.rb, line 122
def name(c)
   color = Colors.new
   color.name(c)
   color
end
new() click to toggle source
# File lib/graphviz/utils/colors.rb, line 12
def initialize
   @r, @g, @b, @a, @h, @s, @v, @color = nil, nil, nil, nil, nil, nil, nil, nil
end
rgb(r, g, b, a = nil) click to toggle source
# File lib/graphviz/utils/colors.rb, line 110
def rgb(r, g, b, a = nil)
   color = Colors.new
   color.rgb(r, g, b, a)
   color
end
rgb_to_hsv(r, g, b) click to toggle source
# File lib/graphviz/utils/colors.rb, line 128
def rgb_to_hsv(r, g, b)
   h, s, v = 0.0, 0.0, 0.0

   _r = r.convert_base(16, 10).to_f / 255.0
   _g = g.convert_base(16, 10).to_f / 255.0
   _b = b.convert_base(16, 10).to_f / 255.0
   rgb = [ _r, _g, _b ]

   min = rgb.min
   max = rgb.max
   v = max

   delta = max - min
   if max != 0.0
      s = delta / max
   else
      return [-1, 0, v]
   end

   if _r == max
      h = ( _g - _b ) / delta
   elsif( _g == max )
      h = 2 + ( _b - _r ) / delta
   else
      h = 4 + ( _r - _g ) / delta
   end

   h = h * 60
   h = h + 360 if h < 0
   h = h / 360.0

   [h, s, v]
end

Public Instance Methods

hsv(h, s, v) click to toggle source
# File lib/graphviz/utils/colors.rb, line 55
def hsv(h, s, v)
   unless h.is_a?(Float) and s.is_a?(Float) and v.is_a?(Float)
      raise ColorException, "Bas HSV value"
   end

   @h = h
   @s = s
   @v = v

   @r, @g, @b = hsv_to_rgb(@h, @s, @v)

   @color = COLORS.key(rgba_string.downcase);
end
hsv_string(s = ", ") click to toggle source
# File lib/graphviz/utils/colors.rb, line 93
def hsv_string(s = ", ")
   unless @h.nil?
      "#{@h}#{s}#{@s}#{s}#{@v}"
   else
      nil
   end
end
hsv_to_rgb(h, s, v) click to toggle source
# File lib/graphviz/utils/colors.rb, line 105
def hsv_to_rgb(h, s, v)
   Colors.hsv_to_rgb(h, s, v)
end
name(c = nil) click to toggle source
# File lib/graphviz/utils/colors.rb, line 69
def name(c = nil)
   return @color if c.nil?

   @color = c

   rgb = COLORS[c]
   unless rgb.nil?
      m = RGBA.match(rgb)
      @r = m[1]
      @g = m[2]
      @b = m[3]

      @h, @s, @v = rgb_to_hsv(@r, @g, @b)
   end
end
rgb(r, g, b, a = nil) click to toggle source
# File lib/graphviz/utils/colors.rb, line 16
def rgb(r, g, b, a = nil)
   if r.is_a?(Fixnum)
      r = r.to_s.convert_base(10, 16)
   end
   unless r.is_a?(String) and HEX_FOR_COLOR.match(r)
      raise ColorException, "Bad red value"
   end

   if g.is_a?(Fixnum)
      g = g.to_s.convert_base(10, 16)
   end
   unless g.is_a?(String) and HEX_FOR_COLOR.match(g)
      raise ColorException, "Bad green value"
   end

   if b.is_a?(Fixnum)
      b = b.to_s.convert_base(10, 16)
   end
   unless b.is_a?(String) and HEX_FOR_COLOR.match(b)
      raise ColorException, "Bad blue value"
   end

   if a.is_a?(Fixnum)
      a = a.to_s.convert_base(10, 16)
   end
   unless a.nil? or (a.is_a?(String) and HEX_FOR_COLOR.match(a))
      raise ColorException, "Bad alpha value"
   end

   @r = r
   @g = g
   @b = b
   @a = a

   @color = COLORS.key(rgba_string.downcase)

   @h, @s, @v = rgb_to_hsv(@r, @g, @b)
end
rgb_to_hsv(r, g, b) click to toggle source
# File lib/graphviz/utils/colors.rb, line 101
def rgb_to_hsv(r, g, b)
   Colors.rgb_to_hsv(r, g, b)
end
rgba_string(c = "") click to toggle source
# File lib/graphviz/utils/colors.rb, line 85
def rgba_string(c = "")
   unless @r.nil?
      "#{c}#{@r}#{@g}#{@b}#{((@a.nil?)?"":@a)}"
   else
      nil
   end
end