palette.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * 调色板
  3. */
  4. define(function(require, exports, module) {
  5. //标准color
  6. var StandardColor = require('./standardcolor'),
  7. Color = require('./color'),
  8. Utils = require('../core/utils');
  9. var Palette = require('../core/class').createClass('Palette', {
  10. constructor: function() {
  11. this.color = {};
  12. },
  13. /*
  14. * 获取颜色名称所对应的颜色值的Color对象
  15. * @param name 需要获取的颜色名称
  16. * @return 对应颜色名称的color对象, 如果未找到对应的名称, 则返回null
  17. */
  18. get: function(name) {
  19. var colorValue = this.color[name] ||
  20. StandardColor.EXTEND_STANDARD[name] ||
  21. StandardColor.COLOR_STANDARD[name] || '';
  22. if (colorValue) {
  23. return new Color(colorValue);
  24. }
  25. return null;
  26. },
  27. /*
  28. * 获取给定名称的颜色的hex值表示
  29. * @param name 需要获取的颜色名称
  30. * @return 如果找到对应的名称, 则返回该名称所对应的hex格式的值, 否则, 返回一个空字符串
  31. */
  32. getColorValue: function(name) {
  33. return this.color[name] || StandardColor.EXTEND_STANDARD[name] || StandardColor.COLOR_STANDARD[name] || '';
  34. },
  35. /*
  36. * 向调色板实例添加自己独有的颜色名称,对已存在的颜色名称, 将会覆盖掉
  37. * @param name 新添加的颜色名称
  38. * @param value 新添加的颜色名称所对应的值, 可以是一个合法的颜色字符串或者是一个color对象
  39. * @return 新添加的颜色的值
  40. */
  41. add: function(name, value) {
  42. if (typeof value === 'string') {
  43. this.color[name] = new Color(value).toRGBA();
  44. } else {
  45. this.color[name] = value.toRGBA();
  46. }
  47. return value;
  48. },
  49. /*
  50. * 删除调色板实例上用户自己添加的颜色, 该方法不能删除内置的颜色
  51. * @param name 需要删除的颜色名称
  52. * @return 删除是否成功的bool值
  53. */
  54. remove: function(name) {
  55. if (this.color.hasOwnProperty(name)) {
  56. delete this.color[name];
  57. return true;
  58. }
  59. return false;
  60. }
  61. });
  62. Utils.extend(Palette, {
  63. getColor: function(name) {
  64. var colorValue = StandardColor.EXTEND_STANDARD[name] || StandardColor.COLOR_STANDARD[name];
  65. if (colorValue) {
  66. return new Color(colorValue);
  67. }
  68. return null;
  69. },
  70. /*
  71. * 通过给定的名字获取标准的颜色值表示, 返回的值以hex的方式提供
  72. * @param name 需要获取的标准颜色名称
  73. * @return 名字所对应的颜色值的hex表示, 如果未找到对应名称的值, 则返回一个空字符串
  74. */
  75. getColorValue: function(name) {
  76. return StandardColor.EXTEND_STANDARD[name] || StandardColor.COLOR_STANDARD[name] || '';
  77. },
  78. /*
  79. * 向调色板添加颜色名称,新添加的颜色对所有的调色板对象都可见
  80. * 对已存在的颜色名称, 将会覆盖掉
  81. * @param name 新添加的颜色名称
  82. * @param value 新添加的颜色名称所对于的值, 应该是一个hex格式的颜色字符串, 如: ”#ff0000“
  83. * @return 新添加的颜色的值
  84. */
  85. addColor: function(name, value) {
  86. if (typeof value === 'string') {
  87. StandardColor.EXTEND_STANDARD[name] = new Color(value).toRGBA();
  88. } else {
  89. StandardColor.EXTEND_STANDARD[name] = value.toRGBA();
  90. }
  91. return value;
  92. },
  93. /*
  94. * 删除用户自己添加的颜色, 该方法不能删除内置的颜色, 该方法不会影响调色板实例自由的颜色
  95. * @param name 需要删除的颜色名称
  96. * @return 删除是否成功的bool值
  97. */
  98. removeColor: function(name) {
  99. if (StandardColor.EXTEND_STANDARD.hasOwnProperty(name)) {
  100. delete StandardColor.EXTEND_STANDARD[name];
  101. return true;
  102. }
  103. return false;
  104. }
  105. });
  106. return Palette;
  107. });