/timeplot/scripts/color.js

http://showslow.googlecode.com/ · JavaScript · 152 lines · 85 code · 13 blank · 54 comment · 17 complexity · c592069411b571837e6fc7073287e23c MD5 · raw file

  1. /**
  2. * Color
  3. *
  4. * @fileOverview Color
  5. * @name Color
  6. */
  7. /*
  8. * Inspired by Plotr
  9. * Copyright 2007 (c) Bas Wenneker <sabmann[a]gmail[d]com>
  10. * For use under the BSD license. <http://www.solutoire.com/plotr>
  11. */
  12. /**
  13. * Create a Color object that can be used to manipulate colors programmatically.
  14. */
  15. Timeplot.Color = function(color) {
  16. this._fromHex(color);
  17. };
  18. Timeplot.Color.prototype = {
  19. /**
  20. * Sets the RGB values of this coor
  21. *
  22. * @param {Number} r,g,b Red green and blue values (between 0 and 255)
  23. */
  24. set: function (r,g,b,a) {
  25. this.r = r;
  26. this.g = g;
  27. this.b = b;
  28. this.a = (a) ? a : 1.0;
  29. return this.check();
  30. },
  31. /**
  32. * Set the color transparency
  33. *
  34. * @param {float} a Transparency value, between 0.0 (fully transparent) and 1.0 (fully opaque).
  35. */
  36. transparency: function(a) {
  37. this.a = a;
  38. return this.check();
  39. },
  40. /**
  41. * Lightens the color.
  42. *
  43. * @param {integer} level Level to lighten the color with.
  44. */
  45. lighten: function(level) {
  46. var color = new Timeplot.Color();
  47. return color.set(
  48. this.r += parseInt(level, 10),
  49. this.g += parseInt(level, 10),
  50. this.b += parseInt(level, 10)
  51. );
  52. },
  53. /**
  54. * Darkens the color.
  55. *
  56. * @param {integer} level Level to darken the color with.
  57. */
  58. darken: function(level){
  59. var color = new Timeplot.Color();
  60. return color.set(
  61. this.r -= parseInt(level, 10),
  62. this.g -= parseInt(level, 10),
  63. this.b -= parseInt(level, 10)
  64. );
  65. },
  66. /**
  67. * Checks and validates if the hex values r, g and b are
  68. * between 0 and 255.
  69. */
  70. check: function() {
  71. if (this.r > 255) {
  72. this.r = 255;
  73. } else if (this.r < 0){
  74. this.r = 0;
  75. }
  76. if (this.g > 255) {
  77. this.g = 255;
  78. } else if (this.g < 0) {
  79. this.g = 0;
  80. }
  81. if (this.b > 255){
  82. this.b = 255;
  83. } else if (this.b < 0){
  84. this.b = 0;
  85. }
  86. if (this.a > 1.0){
  87. this.a = 1.0;
  88. } else if (this.a < 0.0){
  89. this.a = 0.0;
  90. }
  91. return this;
  92. },
  93. /**
  94. * Returns a string representation of this color.
  95. *
  96. * @param {float} alpha (optional) Transparency value, between 0.0 (fully transparent) and 1.0 (fully opaque).
  97. */
  98. toString: function(alpha) {
  99. var a = (alpha) ? alpha : ((this.a) ? this.a : 1.0);
  100. return 'rgba(' + this.r + ',' + this.g + ',' + this.b + ',' + a + ')';
  101. },
  102. /**
  103. * Returns the hexadecimal representation of this color (without the alpha channel as hex colors don't support it)
  104. */
  105. toHexString: function() {
  106. return "#" + this._toHex(this.r) + this._toHex(this.g) + this._toHex(this.b);
  107. },
  108. /*
  109. * Parses and stores the hex values of the input color string.
  110. *
  111. * @param {String} color Hex or rgb() css string.
  112. */
  113. _fromHex: function(color) {
  114. if(/^#?([\da-f]{3}|[\da-f]{6})$/i.test(color)){
  115. color = color.replace(/^#/, '').replace(/^([\da-f])([\da-f])([\da-f])$/i, "$1$1$2$2$3$3");
  116. this.r = parseInt(color.substr(0,2), 16);
  117. this.g = parseInt(color.substr(2,2), 16);
  118. this.b = parseInt(color.substr(4,2), 16);
  119. } else if(/^rgb *\( *\d{0,3} *, *\d{0,3} *, *\d{0,3} *\)$/i.test(color)){
  120. color = color.match(/^rgb *\( *(\d{0,3}) *, *(\d{0,3}) *, *(\d{0,3}) *\)$/i);
  121. this.r = parseInt(color[1], 10);
  122. this.g = parseInt(color[2], 10);
  123. this.b = parseInt(color[3], 10);
  124. }
  125. this.a = 1.0;
  126. return this.check();
  127. },
  128. /*
  129. * Returns an hexadecimal representation of a 8 bit integer
  130. */
  131. _toHex: function(dec) {
  132. var hex = "0123456789ABCDEF"
  133. if (dec < 0) return "00";
  134. if (dec > 255) return "FF";
  135. var i = Math.floor(dec / 16);
  136. var j = dec % 16;
  137. return hex.charAt(i) + hex.charAt(j);
  138. }
  139. };