Advertisement
Guest User

Untitled

a guest
May 26th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1.  
  2. function hexToRgb(hex) {
  3. // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  4. var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  5. hex = hex.replace(shorthandRegex, function(m, r, g, b) {
  6. return r + r + g + g + b + b;
  7. });
  8.  
  9. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  10. return result ? {
  11. r: parseInt(result[1], 16),
  12. g: parseInt(result[2], 16),
  13. b: parseInt(result[3], 16)
  14. } : null;
  15. }
  16.  
  17. /*
  18. * Author : Baptiste Leulliette
  19. * Action : Retourne la couleur décimale de la couleur Hexa.
  20. * Date : 26 / 05
  21. */
  22. function getBritghtness(R, G, B) {
  23. /*console.log (R + " " + G + " " + B);
  24. var red = R / 255;
  25. var green = G / 255;
  26. var blue = B / 255;
  27. if (red <= 0.03928) {
  28. red = red / 12.92;
  29. } else {
  30. if (red != 1) {
  31. red = Math.Pow (((red +0.055) / 1.055), 2.4);
  32. }
  33.  
  34. }
  35. if (blue <= 0.03928) {
  36. blue = blue / 12.92;
  37. } else {
  38. if (blue != 1) {
  39. blue = Math.Pow (((blue +0.055) / 1.055), 2.4);
  40. }
  41.  
  42. }
  43. if (green <= 0.03928) {
  44. green = green / 12.92;
  45. } else {
  46. if (green != 1 ) {
  47. green = Math.Pow (((green +0.055) / 1.055), 2.4);
  48. }
  49.  
  50. }
  51. console.log("Sortie");
  52. var brightness = (0.2126 * red) + (0.7152 * green) + (0.0722 * blue);
  53. return brightness;*/
  54.  
  55. return ((red * 299) + (Green * 587) + (Blue * 114)) / 1000;
  56. }
  57.  
  58. /*
  59. * Author : Baptiste Leulliette
  60. * Action : Calcule la difference de couleur entre la couleur de fond et le texte
  61. * Date : 26 / 05
  62. */
  63. function DisplayContrast() {
  64. var cPolice;
  65. var cFond;
  66. switch (color) {
  67. case "black":
  68. cPolice = "#000000";
  69. break;
  70. case "blue":
  71. cPolice = "#0000FF";
  72. break;
  73. case "yellow":
  74. cPolice = "#FFFF00";
  75. break;
  76. case "white":
  77. cPolice = "#FFFFFF";
  78. break;
  79. case "green":
  80. cPolice = "#008800";
  81. break;
  82. default:
  83. break;
  84. }
  85.  
  86. switch (background) {
  87. case "black":
  88. cFond = "#000000";
  89. break;
  90. case "blue":
  91. cFond = "#0000FF";
  92. break;
  93. case "yellow":
  94. cFond = "#FFFF00";
  95. break;
  96. case "white":
  97. cFond = "#FFFFFF";
  98. break;
  99. case "green":
  100. cFond = "#008800";
  101. break;
  102. default:
  103. break;
  104. }
  105.  
  106. //get des couleurs OK
  107. var hexPolice = hexToRgb(cPolice);
  108. var hexFond = hexToRgb(cFond);
  109. /*var lumPolice = getBritghtness(hexPolice.r, hexPolice.g, hexPolice.b);
  110. var lumFond = getBritghtness(hexFond.r, hexFond.g, hexFond.b);
  111. //Formule : Constraste = Absolute(lumFond - lumPolice) / lumFond;
  112. console.log("Police : " + lumPolice);
  113. console.log("fond : " + lumFond);*/
  114. 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));
  115. console.log(total);
  116. console.log("Ratio (en %) : " + total*100/765);
  117. console.log("Ratio suffisant (flat) = 765");
  118. console.log("Ratio suffisant (en %) = 84.96%");
  119. }
  120.  
  121. function maximum (i1, i2) {
  122. if (i1 >= i2) {
  123. return i1;
  124. }
  125. return i2;
  126. }
  127.  
  128. function minimum (i1, i2) {
  129. if (i1 >= i2) {
  130. return i2;
  131. }
  132. return i1;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement