1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
// -------------------- Color Picker --------------------
var idColor = "";
// Différentes fonctions nécessaire au Color Picker (menu du choix de la couleur)
var colorPicker = {
// Configuration
idSV : "canvasSV", // id du canvas affichant la saturation et la valeur
idT : "canvasT", // id du canvas affichant la teinte
idO : "canvasO", // id du canvas affichant l'opacité
width : 250, // largeur
height : 250, // hauteur
rayonRonds : 5, // rayon des ronds
ombreActive : "0px 0px 3px rgba(150,200,255,1), 0px 0px 8px rgba(64,190,255,1)",
ombreInactive : "0px 0px 5px rgba(64,64,64,0.4)",
// Variables définies lors de l'initialisation
canvasSV : null,
canvasT : null,
canvasO : null,
ctxSV : null,
ctxT : null,
ctxO : null,
lingradS : null,
lingradV : null,
lingradT : null,
// Couleurs
backgroundColor : "rgb(193,255,0)",
saturation : 100,
valeur : 100,
teinte : 75,
rouge : 193,
vert : 255,
bleu : 0,
opacity: 1,
// Couleurs RGB sans application de la saturation et de la valeur
r : 255,
g : 0,
b : 0,
// Autres variables
sourisDown : false, // Indique si on clique sur le colorPicker ou pas
sourisDehors : true, // Indique si la souris est en-dehors du colorPicker lors d'un clique
idColor : null,
// Fonction d'initialisation
init : function(id){
// Récupérer les éléments
this.canvasSV = document.getElementById(this.idSV);
this.canvasT = document.getElementById(this.idT);
this.canvasO = document.getElementById(this.idO);
// Définir la taille
this.canvasSV.width = this.width;
this.canvasSV.height = this.height;
this.canvasSV.style.width = this.width + "px";
this.canvasSV.style.height = this.height + "px";
this.canvasT.width = this.width/10;
this.canvasT.height = this.height;
this.canvasT.style.width = this.width/10 + "px";
this.canvasT.style.height = this.height + "px";
this.canvasO.width = this.width*1.15;
this.canvasO.height = this.height/10;
this.canvasO.style.width = this.width*1.15 + "px";
this.canvasO.style.height = this.height/10 + "px";
// Initialisation canvas
this.ctxSV = this.canvasSV.getContext("2d");
this.ctxT = this.canvasT.getContext("2d");
this.ctxO = this.canvasO.getContext("2d");
// Création des dégradés
this.lingradV = this.ctxSV.createLinearGradient(0, 0, 0, this.height);
this.lingradV.addColorStop(0, 'rgba(255,255,255,0)');
this.lingradV.addColorStop(1, 'rgba(255,255,255,1)');
this.lingradS = this.ctxSV.createLinearGradient(0, 0, this.width, 0);
this.lingradS.addColorStop(0, 'rgba(0,0,0,1)');
this.lingradS.addColorStop(1, 'rgba(0,0,0,0)');
this.lingradT = this.ctxT.createLinearGradient(0, 0, 0, this.height);
this.lingradT.addColorStop(0, 'rgb(255,0,0)');
this.lingradT.addColorStop(1/6, 'rgb(255,255,0)');
this.lingradT.addColorStop(2/6, 'rgb(0,255,0)');
this.lingradT.addColorStop(3/6, 'rgb(0,255,255)');
this.lingradT.addColorStop(4/6, 'rgb(0,0,255)');
this.lingradT.addColorStop(5/6, 'rgb(255,0,255)');
this.lingradT.addColorStop(1, 'rgb(255,0,0)');
// Événements roulette (initialisé seulement une fois)
if(window.addEventListener && !this.idColor){
this.canvasSV.addEventListener('DOMMouseScroll', function(event){colorPicker.eventWheel(event, colorPicker.idSV)}, false);
this.canvasSV.onmousewheel = function(event){colorPicker.eventWheel(event, colorPicker.idSV)};
this.canvasT.addEventListener('DOMMouseScroll', function(event){colorPicker.eventWheel(event, colorPicker.idT)}, false);
this.canvasT.onmousewheel = function(event){colorPicker.eventWheel(event, colorPicker.idT)};
this.canvasO.addEventListener('DOMMouseScroll', function(event){colorPicker.eventWheel(event, colorPicker.idO)}, false);
this.canvasO.onmousewheel = function(event){colorPicker.eventWheel(event, colorPicker.idO)};
}
// Définir la couleur
this.idColor = id;
var couleur = eval(document.getElementById(this.idColor).title);
// alert(id+" ; "+couleur)
document.getElementById("apercuCouleur").style.backgroundColor = couleur;
document.getElementById("apercuCouleur2").style.backgroundColor = couleur;
this.definirCouleur(couleur);
// Dessiner
this.dessiner();
this.dessinerApercu();
// Définir les valeurs des inputs
this.definirInputs();
// Définir le style de l'ombre
this.sourisOut();
},
definirCouleur : function(colorRGB){
var table = /(.*?)rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/.exec(colorRGB);
if (table == null){
table = /(.*?)rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/.exec(colorRGB);
}
if(table == null){
return "";
}
this.rouge = parseInt(table[2]);
this.vert = parseInt(table[3]);
this.bleu = parseInt(table[4]);
this.RGB_SVT();
},
sourisClick : function(id){
this.sourisDown = id;
if(id != this.idO){
document.getElementById(id).style.boxShadow = this.ombreActive;
}
},
sourisOut : function(){
this.canvasSV.style.boxShadow = this.ombreInactive;
this.canvasT.style.boxShadow = this.ombreInactive;
},
sourisOver : function(id){
if(this.sourisDown == id && id != this.idO){
document.getElementById(id).style.boxShadow = this.ombreActive;
}
},
sourisUp : function(){
this.sourisDown = false;
this.canvasSV.style.boxShadow = this.ombreInactive;
this.canvasT.style.boxShadow = this.ombreInactive;
this.dessinerO();
},
eventWheel : function(event, id){
if(!event) event = window.event;
if(event.wheelDelta){
if(event.wheelDelta < 0){
colorPicker.sourisWheelUp(id);
}
else{
colorPicker.sourisWheelDown(id);
}
}
else if(event.detail){
if(event.detail > 0){
colorPicker.sourisWheelUp(id);
}
else{
colorPicker.sourisWheelDown(id);
}
}
},
sourisWheelUp : function(id){
switch(id){
case this.idT:
colorPicker.ajouterT(5);
break;
case this.idO:
colorPicker.ajouterO(0.1);
break;
case this.idSV:
colorPicker.ajouterS(-5);
break;
}
},
sourisWheelDown : function(id){
switch(id){
case this.idT:
colorPicker.ajouterT(-5);
break;
case this.idO:
colorPicker.ajouterO(-0.1);
break;
case this.idSV:
colorPicker.ajouterS(5);
break;
}
},
ajouterT : function(nbr){
this.teinte += nbr;
if(this.teinte < 0){
this.teinte = 0;
}
else if(this.teinte > 360){
this.teinte = 360;
}
this.SVT_RGB();
},
ajouterO : function(nbr){
this.opacity = Math.round((this.opacity+nbr)*100)/100;
if(this.opacity < 0){
this.opacity = 0;
}
else if(this.opacity > 1){
this.opacity = 1;
}
this.SVT_RGB();
},
ajouterS : function(nbr){
this.saturation += nbr;
if(this.saturation < 0){
this.saturation = 0;
}
else if(this.saturation > 100){
this.saturation = 100;
}
this.SVT_RGB();
},
// Lors du déplacement de la souris
moveSV : function(event){
// Vérifie si on appuie sur la souris
if(this.sourisDown != this.idSV){
return 0;
}
var element = this.canvasSV;
var posDivY = 0;
var posDivX = 0;
// Récupérer la position du canvas par rapport à la page
while(element){
posDivY = posDivY + element.offsetTop;
posDivX = posDivX + element.offsetLeft;
element = element.offsetParent;
}
// Définir la saturation et la valeur à partir de la position de la souris
this.saturation = 100-Math.round((event.clientY - posDivY -1)/(this.height+1)*100);
this.valeur = Math.round((event.clientX - posDivX -1)/(this.width+1)*100);
this.SVT_RGB();
},
moveT : function(event){
if(this.sourisDown != this.idT){
return 0;
}
var element = this.canvasT;
var posDivY = 0;
while(element){
posDivY = posDivY + element.offsetTop;
element = element.offsetParent;
}
this.teinte = Math.round( (event.clientY - posDivY -0) / (this.height+1)*360);
this.SVT_RGB();
},
SVT_RGB : function(){
this.T_rgb();
this.rouge = Math.round((this.r + (255-this.r) * (-1) * (this.saturation-100) / 100 )* this.valeur / 100);
this.vert = Math.round((this.g + (255-this.g) * (-1) * (this.saturation-100) / 100 )* this.valeur / 100);
this.bleu = Math.round((this.b + (255-this.b) * (-1) * (this.saturation-100) / 100 )* this.valeur / 100);
this.definirInputs();
this.dessinerApercu();
this.dessiner();
},
T_rgb : function(){
var r,g,b = 0;
var T = this.teinte;
if (T<60){
r = 255;
g = T/60*255;
b = 0;
}
else if (T<120){
r = (255-(T%60/60*255))%256;
g = 255;
b = 0;
}
else if (T<180){
r = 0;
g = 255;
b = T%60/60*255;
}
else if (T<240){
r = 0;
g = (255-(T%60/60*255))%256;
b = 255;
}
else if (T<300){
r = T%60/60*255;
g = 0;
b = 255;
}
else if (T<360){
r = 255;
g = 0;
b = (255-(T%60/60*255))%256;
}
else{
r = 255;
g = 0;
b = 0;
}
this.r = Math.round(r);
this.g = Math.round(g);
this.b = Math.round(b);
},
RGB_SVT : function(){
// Voir http://fr.wikipedia.org/wiki/Teinte_Saturation_Valeur#Conversion_de_RVB_vers_TSV
var r = this.rouge/255;
var g = this.vert/255;
var b = this.bleu/255;
if(!isFinite(r)){
r = 0;
}
if(!isFinite(g)){
g = 0;
}
if(!isFinite(b)){
b = 0;
}
var max = Math.max(r,g,b);
var min = Math.min(r,g,b);
var s, v, t;
// Teinte
switch(max){
case r:
t = (60 * (g-b)/(max-min) + 360) % 360;
break;
case g:
t = 60 * (b-r)/(max-min) + 120;
break;
case b:
t = 60 * (r-g)/(max-min) + 240;
break;
default: /* case min: */
t = 0;
break;
}
// Saturation
if(max == 0){
s = 0;
}
else{
s = 1-(min/max);
}
// Valeur
v = max;
// Définir les variables
this.saturation = s*100;
this.valeur = v*100;
this.teinte = Math.round(t);
this.T_rgb();
},
dessiner : function(){
var ctxSV = this.ctxSV;
var ctxT = this.ctxT;
// Fond
ctxSV.fillStyle = "rgb("+this.r+","+this.g+","+this.b+")";
ctxSV.fillRect(0, 0, this.width, this.height);
// Dégradés
ctxSV.fillStyle = this.lingradV;
ctxSV.fillRect(0, 0, this.width, this.height);
ctxSV.fillStyle = this.lingradS;
ctxSV.fillRect(0, 0, this.width, this.height);
// Souris
var x = Math.round(this.width*this.valeur/100);
var y = Math.round(this.height-this.height*this.saturation/100);
ctxSV.beginPath();
ctxSV.arc(x, y, this.rayonRonds, 0, 2*Math.PI, true);
ctxSV.strokeStyle = "rgba(255,255,255,0.8)";
ctxSV.shadowOffsetX = 1;
ctxSV.shadowOffsetY = 1;
ctxSV.shadowColor = "rgba(0,0,0,1)";
ctxSV.shadowBlur = 2;
ctxSV.lineWidth = 1.5;
ctxSV.stroke();
// Fond Teinte
ctxT.fillStyle = this.lingradT;
ctxT.fillRect(0, 0, this.width/10, this.height);
// Souris Teinte
var pos = Math.round(0.99*this.height*this.teinte/360);
ctxT.fillStyle = "rgba(255,255,255,0.8)";
ctxT.shadowOffsetX = 0;
ctxT.shadowOffsetY = 0;
ctxT.shadowColor = "rgba(0,0,0,1)";
ctxT.shadowBlur = 3;
ctxT.fillRect(0, pos, this.width/10, 2);
// Désactiver les ombres
ctxSV.shadowColor = "rgba(0,0,0,0)";
ctxT.shadowColor = "rgba(0,0,0,0)";
this.dessinerO();
},
dessinerApercu : function(){
document.getElementById("apercuCouleur").style.backgroundColor = "rgba("+this.rouge+","+this.vert+","+this.bleu+","+this.opacity+")";
},
definirInputs : function(){
document.getElementById("inputValeur").value = this.valeur;
document.getElementById("inputSaturation").value = this.saturation;
document.getElementById("inputTeinte").value = this.teinte;
document.getElementById("inputRouge").value = this.rouge;
document.getElementById("inputVert").value = this.vert;
document.getElementById("inputBleu").value = this.bleu;
document.getElementById("inputOpacity").value = this.opacity;
},
recupererInputs : function(){
this.valeur = parseInt(document.getElementById("inputValeur").value);
this.saturation = parseInt(document.getElementById("inputSaturation").value);
this.teinte = parseInt(document.getElementById("inputTeinte").value);
this.opacity = parseFloat(document.getElementById("inputOpacity").value);
this.SVT_RGB();
this.definirInputs();
this.dessinerApercu();
this.dessiner();
},
recupererInputs2 : function(){
this.rouge = parseInt(document.getElementById("inputRouge").value);
this.vert = parseInt(document.getElementById("inputVert").value);
this.bleu = parseInt(document.getElementById("inputBleu").value);
this.RGB_SVT();
this.definirInputs();
this.dessinerApercu();
this.dessiner();
},
moveO : function(event){
if(this.sourisDown != this.idO){
return 0;
}
var element = this.canvasO;
var posDivX = 0;
while(element){
posDivX = posDivX + element.offsetLeft;
element = element.offsetParent;
}
this.opacity = Math.round( (event.clientX - posDivX -this.width/10) / (this.width*0.9)*100)/100;
if(this.opacity < 0){
this.opacity = 0;
}
else if(this.opacity > 1){
this.opacity = 1;
}
this.dessinerO();
this.definirInputs();
this.dessinerApercu();
},
dessinerO : function(){
var ctx = this.ctxO;
ctx.shadowBlur = 2;
// Ligne
if(this.sourisDown == this.idO){
ctx.shadowColor = "rgba(0,50,100,1)";
}
else{
ctx.shadowColor = "rgba(0,0,0,1)";
}
ctx.globalAlpha = 1;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
ctx.fillStyle = "rgba("+this.rouge+","+this.vert+","+this.bleu+",1)";
ctx.clearRect(0,0,this.width*1.15,this.height);
ctx.fillRect(this.width/10, this.height/20-1, this.width*0.9, 3);
// Rond
var x = this.width/10 + this.width*0.9*this.opacity-1;
var y = this.height/20;
if(this.sourisDown == this.idO){
ctx.fillStyle = "rgba(0,40,80,1)";
ctx.strokeStyle = "rgba(230,250,255,0.8)";
}
else{
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.strokeStyle = "rgba(255,255,255,0.8)";
}
ctx.globalAlpha = 0.1+this.opacity*0.9;
ctx.beginPath();
ctx.arc(x, y, this.rayonRonds+1, 0, 2*Math.PI, true);
ctx.shadowColor = "rgba(0,0,0,1)";
ctx.shadowOffsetX = 1;
ctx.shadowOffsetY = 1;
ctx.lineWidth = 1;
ctx.fill();
ctx.stroke();
// Petits ronds
ctx.globalAlpha = 1;
ctx.shadowColor = "rgba(0,0,0,1)";
ctx.beginPath();
ctx.arc(8, this.height/20+0.5, this.rayonRonds, 0, 2*Math.PI, true);
ctx.strokeStyle = "rgba(0,0,0,0.5)";
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
ctx.lineWidth = 1;
ctx.stroke();
ctx.beginPath();
ctx.arc(this.width*1.08, this.height/20+0.5, this.rayonRonds, 0, 2*Math.PI, true);
ctx.strokeStyle = "rgba(0,0,0,0.8)";
ctx.fillStyle = "rgba(0,0,0,0.6)";
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
ctx.lineWidth = 1;
ctx.stroke();
ctx.fill();
},
exporterCouleur : function(){
eval(document.getElementById(this.idColor).title+' = "rgba(" + this.rouge +", " + this.vert + ", " + this.bleu +", "+this.opacity+")"');
document.getElementById(this.idColor).style.backgroundColor = eval(document.getElementById(this.idColor).title);
},
fermer : function(){
colorPicker.exporterCouleur();
if(colorPicker.idColor == 'buttonColor'){
cacherMenu();
}
else if(dernierMenu == "menuFonctions"){
editeur.getOptions();
afficherMenu(dernierMenu);
}
else{
cacherMenu();
}
actualiserGraph();
}
};
|