Show More
@@ -44,6 +44,75 b' String.prototype.format = function() {' | |||||
44 |
|
44 | |||
45 | }(); |
|
45 | }(); | |
46 |
|
46 | |||
|
47 | ||||
|
48 | /** | |||
|
49 | * SmartColorGenerator | |||
|
50 | * | |||
|
51 | *usage:: | |||
|
52 | * var CG = new ColorGenerator(); | |||
|
53 | * var col = CG.getColor(key); //returns array of RGB | |||
|
54 | * 'rgb({0})'.format(col.join(',') | |||
|
55 | * | |||
|
56 | * @returns {ColorGenerator} | |||
|
57 | */ | |||
|
58 | function ColorGenerator(){ | |||
|
59 | this.GOLDEN_RATIO = 0.618033988749895; | |||
|
60 | this.CURRENT_RATIO = 0.22717784590367374 // this can be random | |||
|
61 | this.HSV_1 = 0.75;//saturation | |||
|
62 | this.HSV_2 = 0.95; | |||
|
63 | this.color; | |||
|
64 | this.cacheColorMap = {}; | |||
|
65 | }; | |||
|
66 | ||||
|
67 | ColorGenerator.prototype = { | |||
|
68 | getColor:function(key){ | |||
|
69 | if(this.cacheColorMap[key] !== undefined){ | |||
|
70 | return this.cacheColorMap[key]; | |||
|
71 | } | |||
|
72 | else{ | |||
|
73 | this.cacheColorMap[key] = this.generateColor(); | |||
|
74 | return this.cacheColorMap[key]; | |||
|
75 | } | |||
|
76 | }, | |||
|
77 | _hsvToRgb:function(h,s,v){ | |||
|
78 | if (s == 0.0) | |||
|
79 | return [v, v, v]; | |||
|
80 | i = parseInt(h * 6.0) | |||
|
81 | f = (h * 6.0) - i | |||
|
82 | p = v * (1.0 - s) | |||
|
83 | q = v * (1.0 - s * f) | |||
|
84 | t = v * (1.0 - s * (1.0 - f)) | |||
|
85 | i = i % 6 | |||
|
86 | if (i == 0) | |||
|
87 | return [v, t, p] | |||
|
88 | if (i == 1) | |||
|
89 | return [q, v, p] | |||
|
90 | if (i == 2) | |||
|
91 | return [p, v, t] | |||
|
92 | if (i == 3) | |||
|
93 | return [p, q, v] | |||
|
94 | if (i == 4) | |||
|
95 | return [t, p, v] | |||
|
96 | if (i == 5) | |||
|
97 | return [v, p, q] | |||
|
98 | }, | |||
|
99 | generateColor:function(){ | |||
|
100 | this.CURRENT_RATIO = this.CURRENT_RATIO+this.GOLDEN_RATIO; | |||
|
101 | this.CURRENT_RATIO = this.CURRENT_RATIO %= 1; | |||
|
102 | HSV_tuple = [this.CURRENT_RATIO, this.HSV_1, this.HSV_2] | |||
|
103 | RGB_tuple = this._hsvToRgb(HSV_tuple[0],HSV_tuple[1],HSV_tuple[2]); | |||
|
104 | function toRgb(v){ | |||
|
105 | return ""+parseInt(v*256) | |||
|
106 | } | |||
|
107 | return [toRgb(RGB_tuple[0]),toRgb(RGB_tuple[1]),toRgb(RGB_tuple[2])]; | |||
|
108 | ||||
|
109 | } | |||
|
110 | } | |||
|
111 | ||||
|
112 | ||||
|
113 | ||||
|
114 | ||||
|
115 | ||||
47 | /** |
|
116 | /** | |
48 | * GLOBAL YUI Shortcuts |
|
117 | * GLOBAL YUI Shortcuts | |
49 | */ |
|
118 | */ |
General Comments 0
You need to be logged in to leave comments.
Login now