Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function hexToRgb(hex) {
- // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
- var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
- hex = hex.replace(shorthandRegex, function(m, r, g, b) {
- return r + r + g + g + b + b;
- });
- var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
- return result ? {
- r: parseInt(result[1], 16),
- g: parseInt(result[2], 16),
- b: parseInt(result[3], 16)
- } : null;
- }
- /*
- * Author : Baptiste Leulliette
- * Action : Retourne la couleur décimale de la couleur Hexa.
- * Date : 26 / 05
- */
- function getBritghtness(R, G, B) {
- /*console.log (R + " " + G + " " + B);
- var red = R / 255;
- var green = G / 255;
- var blue = B / 255;
- if (red <= 0.03928) {
- red = red / 12.92;
- } else {
- if (red != 1) {
- red = Math.Pow (((red +0.055) / 1.055), 2.4);
- }
- }
- if (blue <= 0.03928) {
- blue = blue / 12.92;
- } else {
- if (blue != 1) {
- blue = Math.Pow (((blue +0.055) / 1.055), 2.4);
- }
- }
- if (green <= 0.03928) {
- green = green / 12.92;
- } else {
- if (green != 1 ) {
- green = Math.Pow (((green +0.055) / 1.055), 2.4);
- }
- }
- console.log("Sortie");
- var brightness = (0.2126 * red) + (0.7152 * green) + (0.0722 * blue);
- return brightness;*/
- return ((red * 299) + (Green * 587) + (Blue * 114)) / 1000;
- }
- /*
- * Author : Baptiste Leulliette
- * Action : Calcule la difference de couleur entre la couleur de fond et le texte
- * Date : 26 / 05
- */
- function DisplayContrast() {
- var cPolice;
- var cFond;
- switch (color) {
- case "black":
- cPolice = "#000000";
- break;
- case "blue":
- cPolice = "#0000FF";
- break;
- case "yellow":
- cPolice = "#FFFF00";
- break;
- case "white":
- cPolice = "#FFFFFF";
- break;
- case "green":
- cPolice = "#008800";
- break;
- default:
- break;
- }
- switch (background) {
- case "black":
- cFond = "#000000";
- break;
- case "blue":
- cFond = "#0000FF";
- break;
- case "yellow":
- cFond = "#FFFF00";
- break;
- case "white":
- cFond = "#FFFFFF";
- break;
- case "green":
- cFond = "#008800";
- break;
- default:
- break;
- }
- //get des couleurs OK
- var hexPolice = hexToRgb(cPolice);
- var hexFond = hexToRgb(cFond);
- /*var lumPolice = getBritghtness(hexPolice.r, hexPolice.g, hexPolice.b);
- var lumFond = getBritghtness(hexFond.r, hexFond.g, hexFond.b);
- //Formule : Constraste = Absolute(lumFond - lumPolice) / lumFond;
- console.log("Police : " + lumPolice);
- console.log("fond : " + lumFond);*/
- var total = (maximum(hexPolice.r, hexFond.r) - minimum(hexPolice.r, hexFond.r)) + (maximum(hexPolice.g, hexFond.g) - minimum(hexPolice.g, hexFond.g)) + (maximum(hexPolice.b, hexFond.b) - minimum(hexPolice.b, hexFond.b));
- console.log(total);
- console.log("Ratio (en %) : " + total*100/765);
- console.log("Ratio suffisant (flat) = 765");
- console.log("Ratio suffisant (en %) = 84.96%");
- }
- function maximum (i1, i2) {
- if (i1 >= i2) {
- return i1;
- }
- return i2;
- }
- function minimum (i1, i2) {
- if (i1 >= i2) {
- return i2;
- }
- return i1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement