changeset 1465:ef31d0c6bae9 beta

Added smart color generator to rhodecode.js
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 12 Sep 2011 02:24:52 +0300
parents 31ed19b1fdb9
children ad5a543a9b2f
files rhodecode/public/js/rhodecode.js
diffstat 1 files changed, 69 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/public/js/rhodecode.js	Mon Sep 12 02:23:21 2011 +0300
+++ b/rhodecode/public/js/rhodecode.js	Mon Sep 12 02:24:52 2011 +0300
@@ -44,6 +44,75 @@
 
 }();
 
+
+/**
+ * SmartColorGenerator
+ *
+ *usage::
+ *	var CG = new ColorGenerator();
+ *  var col = CG.getColor(key); //returns array of RGB
+ *  'rgb({0})'.format(col.join(',')
+ * 
+ * @returns {ColorGenerator}
+ */
+function ColorGenerator(){
+	this.GOLDEN_RATIO = 0.618033988749895;
+	this.CURRENT_RATIO = 0.22717784590367374 // this can be random
+	this.HSV_1 = 0.75;//saturation
+	this.HSV_2 = 0.95;
+	this.color;
+	this.cacheColorMap = {};
+};
+
+ColorGenerator.prototype = {
+    getColor:function(key){
+    	if(this.cacheColorMap[key] !== undefined){
+    		return this.cacheColorMap[key];
+    	}
+    	else{
+    		this.cacheColorMap[key] = this.generateColor();
+    		return this.cacheColorMap[key];
+    	}
+    },
+    _hsvToRgb:function(h,s,v){
+        if (s == 0.0)
+            return [v, v, v];
+        i = parseInt(h * 6.0)
+        f = (h * 6.0) - i
+        p = v * (1.0 - s)
+        q = v * (1.0 - s * f)
+        t = v * (1.0 - s * (1.0 - f))
+        i = i % 6
+        if (i == 0) 
+            return [v, t, p]
+        if (i == 1) 
+            return [q, v, p]
+        if (i == 2) 
+            return [p, v, t]
+        if (i == 3)
+            return [p, q, v]
+        if (i == 4) 
+            return [t, p, v]
+        if (i == 5)
+            return [v, p, q]            	
+    },
+    generateColor:function(){
+        this.CURRENT_RATIO = this.CURRENT_RATIO+this.GOLDEN_RATIO;
+        this.CURRENT_RATIO = this.CURRENT_RATIO %= 1;
+        HSV_tuple = [this.CURRENT_RATIO, this.HSV_1, this.HSV_2]
+        RGB_tuple = this._hsvToRgb(HSV_tuple[0],HSV_tuple[1],HSV_tuple[2]);
+        function toRgb(v){
+        	return ""+parseInt(v*256)
+        }
+        return [toRgb(RGB_tuple[0]),toRgb(RGB_tuple[1]),toRgb(RGB_tuple[2])];
+        
+    }
+}
+
+
+
+
+
 /**
  * GLOBAL YUI Shortcuts
  */