Class: CTodo::ColorUtils

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

Constant Summary collapse

RAND_COLORS =

cyan, magenta, blue, yellow, green, red

['ffffff', '00ffff', 'ff00ff', '0000ff', 'ffff00', '00ff00']

Class Method Summary collapse

Class Method Details

.func4rgb(rgb, cs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ctodo/color.rb', line 52

def self.func4rgb(rgb, cs)
	h,s,v = rgb_to_hsv(rgb)
	
	if h.nan?
		return proc { cs.white } if v > 0.5
		return proc { cs.black }
	end
	
	assoc = [
		[proc { cs.red }, 0.0],
		[proc { cs.yellow }, 60.0],
		[proc { cs.green }, 120.0],
		[proc { cs.cyan }, 180.0],
		[proc { cs.blue }, 240.0],
		[proc { cs.magenta }, 300.0],
		[proc { cs.red }, 360.0]
	]
	
	dist = 360.0; callee = assoc[0][0]
	assoc.each do |pair|
		d = (pair[1]-h).abs
		if d < dist
			dist = d
			callee = pair[0]
		end
	end
	
	callee
end

.rgb4string(string) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/ctodo/color.rb', line 104

def self.rgb4string(string)
	sum = 0
	string.each_char do |c|
		sum += c[0] % RAND_COLORS.length
	end	
	RAND_COLORS[sum % RAND_COLORS.length]
end

.rgb_to_hsv(rgb) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ctodo/color.rb', line 82

def self.rgb_to_hsv(rgb)
	r = (rgb & 0xff0000) >> 16
	g = (rgb & 0xff00) >> 8
	b = rgb & 0xff
	r /= 255.0; g /= 255.0; b /= 255.0
	max = [r,g,b].max; min = [r,g,b].min
	
	h = 0 if max == min
	h = 60*(0+(g-b)/(max-min)) if max == r
	h = 60*(2+(b-r)/(max-min)) if max == g
	h = 60*(4+(r-g)/(max-min)) if max == b
	h += 360 if h < 0
	
	s = 0 if max == 0
	s = (max-min)/max if max != 0
	
	[h,s,max]
end