alcatel/static/libraries/nco/couleurs.js
changeset 27 8ca7f2cea729
equal deleted inserted replaced
26:94f586daa623 27:8ca7f2cea729
       
     1 /* -- Object for colors -- */
       
     2 
       
     3 function Couleurs() {	
       
     4 	switch (arguments.length) {
       
     5 	case 1:
       
     6 		if ("number"==typeof(arguments[0])) {
       
     7 			if (10==arguments[0]) {
       
     8 				this.get = d3.scale.category10();
       
     9 			}
       
    10 			else if (20==arguments[0]) {
       
    11 				this.get = d3.scale.category20();
       
    12 			}
       
    13 			else {
       
    14 				var domain = [], quantity = 0, colors = colorbrewer, 
       
    15 					setID, colorID, color;
       
    16 				var sets = ["BrBG", "PiYG", "PRGn", "PuOr", "RdBu", "RdYlBu", "RdYlGn"];
       
    17 				
       
    18 				while (quantity < arguments[0]) {
       
    19 					setID = 	Math.floor(Math.random()*7);
       
    20 					colorID = Math.floor(Math.random()*11);
       
    21 					color = Couleurs.fn.d3Color(
       
    22 							colors[sets[setID]][11][colorID]);
       
    23 					
       
    24 					var available = true;
       
    25 					for (var i = 0; i < quantity; i++) {
       
    26 						if (Math.abs(Couleurs.fn.hue(domain[i])
       
    27 								- Couleurs.fn.hue(color)) < 0.1) {
       
    28 							available = false;
       
    29 						}
       
    30 					}
       
    31 					
       
    32 					if (Couleurs.fn.brightness(color) < 0.9 && available) {
       
    33 						domain.push(color);
       
    34 						++quantity;
       
    35 					}
       
    36 				}
       
    37 				
       
    38 				// Transform into hexadecimal colors
       
    39 				for (var index in domain) {
       
    40 					domain[index] = domain[index].toString();
       
    41 				}
       
    42 				this.get = d3.scale.ordinal().range(domain);
       
    43 			}
       
    44 		}
       
    45 		else if (Array==arguments[0].constructor) {
       
    46 			var inputs = [];
       
    47 			for (var i in arguments[0]) {
       
    48 				inputs.push(i);
       
    49 			}
       
    50 			this.get = d3.scale.ordinal()
       
    51 				.domain(inputs)
       
    52 				.range(arguments[0]);
       
    53 		}
       
    54 		else {
       
    55 			throw "Wrong arguments";
       
    56 		}
       
    57 		break;
       
    58 		
       
    59 	case 3:
       
    60 		this.range = d3.interpolateRgb(arguments[0], arguments[1]);
       
    61 		this.levels = arguments[2];
       
    62 		this.get = this.getFromInterpolation;
       
    63 		break;
       
    64 
       
    65 	default:
       
    66 		throw "Wrong number of arguments";
       
    67 	}
       
    68 }
       
    69 
       
    70 Couleurs.fn = Couleurs.prototype = {
       
    71 	constructor: Couleurs,
       
    72 
       
    73 	d3Color: function(rgbString) {
       
    74 		return d3.rgb(rgbString);
       
    75 	},
       
    76 	
       
    77 	brightness: function(color) {
       
    78 		return (color.r + color.r + color.b + color.g + color.g + color.g)/(6*255);
       
    79 	},
       
    80 	
       
    81 	hue: function(color) {
       
    82 		return Math.sqrt(3) * (color.g - color.b)
       
    83 			/(color.r + color.r - color.g - color.b);
       
    84 	},
       
    85 	
       
    86 	getFromInterpolation: function(index) {
       
    87 		if (index<1) {
       
    88 			return this.range(index);
       
    89 		}
       
    90 		else {
       
    91 			index = (0==index%this.levels)? this.levels: index%this.levels; 
       
    92 			return this.range((index - 1)/(this.levels - 1));
       
    93 		}
       
    94 	}
       
    95 };