color.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. DrawingBoard.Control.Color = DrawingBoard.Control.extend({
  2. name: 'colors',
  3. initialize: function() {
  4. this.initTemplate();
  5. var that = this;
  6. this.$el.on('click', '.drawing-board-control-colors-picker', function(e) {
  7. var color = $(this).attr('data-color');
  8. that.board.setColor(color);
  9. that.$el.find('.drawing-board-control-colors-current')
  10. .css('background-color', color)
  11. .attr('data-color', color);
  12. that.board.ev.trigger('color:changed', color);
  13. that.$el.find('.drawing-board-control-colors-rainbows').addClass('drawing-board-utils-hidden');
  14. e.preventDefault();
  15. });
  16. this.$el.on('click', '.drawing-board-control-colors-current', function(e) {
  17. that.$el.find('.drawing-board-control-colors-rainbows').toggleClass('drawing-board-utils-hidden');
  18. e.preventDefault();
  19. });
  20. $('body').on('click', function(e) {
  21. var $target = $(e.target);
  22. var $relatedButton = $target.hasClass('drawing-board-control-colors-current') ? $target : $target.closest('.drawing-board-control-colors-current');
  23. var $myButton = that.$el.find('.drawing-board-control-colors-current');
  24. var $popup = that.$el.find('.drawing-board-control-colors-rainbows');
  25. if ( (!$relatedButton.length || $relatedButton.get(0) !== $myButton.get(0)) && !$popup.hasClass('drawing-board-utils-hidden') )
  26. $popup.addClass('drawing-board-utils-hidden');
  27. });
  28. },
  29. initTemplate: function() {
  30. var tpl = '<div class="drawing-board-control-inner">' +
  31. '<div class="drawing-board-control-colors-current" style="background-color: {{color}}" data-color="{{color}}"></div>' +
  32. '<div class="drawing-board-control-colors-rainbows">{{rainbows}}</div>' +
  33. '</div>';
  34. var oneColorTpl = '<div class="drawing-board-control-colors-picker" data-color="{{color}}" style="background-color: {{color}}"></div>';
  35. var rainbows = '';
  36. $.each([0.75, 0.5, 0.25], $.proxy(function(key, val) {
  37. var i = 0;
  38. var additionalColor = null;
  39. rainbows += '<div class="drawing-board-control-colors-rainbow">';
  40. if (val == 0.25) additionalColor = this._rgba(0, 0, 0, 1);
  41. if (val == 0.5) additionalColor = this._rgba(150, 150, 150, 1);
  42. if (val == 0.75) additionalColor = this._rgba(255, 255, 255, 1);
  43. rainbows += DrawingBoard.Utils.tpl(oneColorTpl, {color: additionalColor.toString() });
  44. while (i <= 330) {
  45. rainbows += DrawingBoard.Utils.tpl(oneColorTpl, {color: this._hsl2Rgba(this._hsl(i-60, 1, val)).toString() });
  46. i+=30;
  47. }
  48. rainbows += '</div>';
  49. }, this));
  50. this.$el.append( $( DrawingBoard.Utils.tpl(tpl, {color: this.board.color, rainbows: rainbows }) ) );
  51. this.$el.find('.drawing-board-control-colors-rainbows').addClass('drawing-board-utils-hidden');
  52. },
  53. onBoardReset: function(opts) {
  54. this.board.setColor(this.$el.find('.drawing-board-control-colors-current').attr('data-color'));
  55. },
  56. _rgba: function(r, g, b, a) {
  57. return { r: r, g: g, b: b, a: a, toString: function() { return "rgba(" + r +", " + g + ", " + b + ", " + a + ")"; } };
  58. },
  59. _hsl: function(h, s, l) {
  60. return { h: h, s: s, l: l, toString: function() { return "hsl(" + h +", " + s*100 + "%, " + l*100 + "%)"; } };
  61. },
  62. _hex2Rgba: function(hex) {
  63. var num = parseInt(hex.substring(1), 16);
  64. return this._rgba(num >> 16, num >> 8 & 255, num & 255, 1);
  65. },
  66. //conversion function (modified a bit) taken from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
  67. _hsl2Rgba: function(hsl) {
  68. var h = hsl.h/360, s = hsl.s, l = hsl.l, r, g, b;
  69. function hue2rgb(p, q, t) {
  70. if(t < 0) t += 1;
  71. if(t > 1) t -= 1;
  72. if(t < 1/6) return p + (q - p) * 6 * t;
  73. if(t < 1/2) return q;
  74. if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
  75. return p;
  76. }
  77. if (s === 0) {
  78. r = g = b = l; // achromatic
  79. } else {
  80. var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
  81. var p = 2 * l - q;
  82. r = Math.floor( (hue2rgb(p, q, h + 1/3)) * 255);
  83. g = Math.floor( (hue2rgb(p, q, h)) * 255);
  84. b = Math.floor( (hue2rgb(p, q, h - 1/3)) * 255);
  85. }
  86. return this._rgba(r, g, b, 1);
  87. }
  88. });