Module: Colorize

Defined in:
lib/scout/log/color.rb

Class Method Summary collapse

Class Method Details

.colorsObject



11
12
13
# File 'lib/scout/log/color.rb', line 11

def self.colors
  @colors ||= IndiferentHash.setup({green: "#00cd00" , red: "#cd0000" , yellow: "#ffd700" })
end

.colors=(colors) ⇒ Object



7
8
9
# File 'lib/scout/log/color.rb', line 7

def self.colors=(colors)
  @colors = colors
end

.continuous(array, start = "#40324F", eend = "#EABD5D", percent = false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/scout/log/color.rb', line 59

def self.continuous(array, start = "#40324F", eend = "#EABD5D", percent = false) 
  start_color = Color.new from_name(start)
  end_color = Color.new from_name(eend)

  if percent
    array = array.collect{|v| n = v.to_f; n = n > 100 ? 100 : n; n < 0.001 ? 0.001 : n}
  else
    array = array.collect{|v| n = v.to_f; } 
  end
  max = array.max
  min = array.min
  range = max - min
  array.collect do |v|
    ratio = (v.to_f-min) / range
    start_color.blend end_color, ratio
  end
end

.distinct(array) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/scout/log/color.rb', line 92

def self.distinct(array)
  colors = diverging_colors.collect{|c| Color.new c }

  num = array.uniq.length
  times = num / 12

  all_colors = colors.dup
  factor = 0.3 / times
  times.times do
    all_colors.concat  colors.collect{|n| n.darken(factor) }
  end

  value_color = Hash[*array.uniq.zip(all_colors).flatten]

  value_color.values_at *array
end

.diverging_colorsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/scout/log/color.rb', line 19

def self.diverging_colors
  @diverging_colors ||=<<~EOF.split("\n")
    #a6cee3
    #1f78b4
    #b2df8a
    #33a02c
    #fb9a99
    #e31a1c
    #fdbf6f
    #ff7f00
    #cab2d6
    #6a3d9a
    #ffff99
    #b15928
  EOF
end

.diverging_colors=(colors) ⇒ Object



15
16
17
# File 'lib/scout/log/color.rb', line 15

def self.diverging_colors=(colors)
  @diverging_colors = colors
end

.from_name(color) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/scout/log/color.rb', line 37

def self.from_name(color)
  return color if color =~ /^#?[0-9A-F]+$/i
  return colors[color.to_s] if colors.include?(color.to_s)

  case color.to_s
  when "white"
    '#000'
  when "black"
    '#fff'
  when 'green'
    colors["green3"] 
  when 'red'
    colors["red3"] 
  when 'yellow'
    colors["gold1"] 
  when 'blue'
    colors["RoyalBlue"] 
  else
    colors[color.to_s] || color
  end
end

.gradient(array, value, start = :green, eend = :red, percent = false) ⇒ Object



77
78
79
80
81
# File 'lib/scout/log/color.rb', line 77

def self.gradient(array, value, start = :green, eend = :red, percent = false)
  index = array.index value
  colors = continuous(array, start, eend, percent)
  colors[index]
end

.rank_gradient(array, value, start = :green, eend = :red, percent = false) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/scout/log/color.rb', line 83

def self.rank_gradient(array, value, start = :green, eend = :red, percent = false)
  index = array.index value
  sorted = array.sort
  array = array.collect{|e| sorted.index e}
  colors = continuous(array, start, eend, percent)
  colors[index]
end

.tsv(tsv, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/scout/log/color.rb', line 109

def self.tsv(tsv, options = {})
  values = tsv.values.flatten
  if Numeric === values.first or (values.first.to_f != 0 and values[0] != "0" and values[0] != "0.0")
    value_colors = IndiferentHash.process_to_hash(values){continuous(values)}
  else
    value_colors = IndiferentHash.process_to_hash(values){distinct(values)}
  end

  if tsv.type == :single
    Hash[*tsv.keys.zip(value_colors.values_at(*values)).flatten]
  else
    Hash[*tsv.keys.zip(values.collect{|vs| value_colors.values_at(*vs)}).flatten]
  end
end