Class: Treemap::GradientColor

Inherits:
ColorBase show all
Defined in:
lib/treemap/gradient_color.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ColorBase

#blue, #green, #red, #to_html, #to_rgb

Constructor Details

#initialize {|_self| ... } ⇒ GradientColor

Returns a new instance of GradientColor.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/treemap/gradient_color.rb', line 18

def initialize
    @min = -100
    @max = 100
    @min_color = "FF0000"   # red
    @mean_color = "000000"  # black
    @max_color = "00FF00"   # green
    @increment = 1

    yield self if block_given?

    # XXX add in error checking. if min >= max, if colors aren't hex, etc.
    @min = @min.to_f
    @max = @max.to_f
    @mean = (@min + @max) / 2.to_f
    @slices = (1.to_f / @increment.to_f) * (@max - @mean).to_f
end

Instance Attribute Details

#incrementObject

Returns the value of attribute increment.



16
17
18
# File 'lib/treemap/gradient_color.rb', line 16

def increment
  @increment
end

#maxObject

Returns the value of attribute max.



16
17
18
# File 'lib/treemap/gradient_color.rb', line 16

def max
  @max
end

#max_colorObject

Returns the value of attribute max_color.



16
17
18
# File 'lib/treemap/gradient_color.rb', line 16

def max_color
  @max_color
end

#mean_colorObject

Returns the value of attribute mean_color.



16
17
18
# File 'lib/treemap/gradient_color.rb', line 16

def mean_color
  @mean_color
end

#minObject

Returns the value of attribute min.



16
17
18
# File 'lib/treemap/gradient_color.rb', line 16

def min
  @min
end

#min_colorObject

Returns the value of attribute min_color.



16
17
18
# File 'lib/treemap/gradient_color.rb', line 16

def min_color
  @min_color
end

Instance Method Details

#get_hex_color(value) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/treemap/gradient_color.rb', line 35

def get_hex_color(value)
    value = @max if(value > @max)
    vaue = @min if(value < @min)


    r1, g1, b1 = to_rgb(@mean_color)
    r2, g2, b2 = to_rgb(@min_color)
    if(value >= @mean) 
        r2, g2, b2 = to_rgb(@max_color)
    end

    rfactor = ((r1 -r2).abs.to_f / @slices) * value.abs 
    gfactor = ((g1 -g2).abs.to_f / @slices) * value.abs
    bfactor = ((b1 -b2).abs.to_f / @slices) * value.abs

    newr = r1 + rfactor
    if(r1 > r2)
        newr = r1 - rfactor
    end

    newg = g1 + gfactor
    if(g1 > g2)
        newg = g1 - gfactor
    end

    newb = b1 + bfactor
    if(b1 > b2)
        newb = b1 - bfactor
    end

    sprintf("%02X", newr.round) + sprintf("%02X", newg.round) + sprintf("%02X", newb.round)
end

#get_rgb_color(value) ⇒ Object



68
69
70
# File 'lib/treemap/gradient_color.rb', line 68

def get_rgb_color(value)
    to_rgb(get_hex_color(value))
end