ColorPicker.hx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package hide.comp;
  2. class ColorPicker extends Component {
  3. public var value(get, set) : Int;
  4. var innerValue : Int;
  5. var mask : Int;
  6. public function new( ?alpha : Bool = false, ?parent : Element, ?root : Element ) {
  7. if( root == null ) root = new Element("<input>");
  8. super(parent,root);
  9. mask = alpha ? -1 : 0xFFFFFF;
  10. (element:Dynamic).spectrum({
  11. showInput : true,
  12. showAlpha : alpha,
  13. showButtons: false,
  14. preferredFormat: alpha ? "hex8" : "hex",
  15. hide : function() {
  16. onClose();
  17. },
  18. show : function() {
  19. onOpen();
  20. },
  21. change : function(color) {
  22. innerValue = Std.parseInt("0x"+color.toHex8()) & mask;
  23. onChange(false);
  24. },
  25. move : function(color) {
  26. innerValue = Std.parseInt("0x"+color.toHex8()) & mask;
  27. onChange(true);
  28. }
  29. });
  30. // cleanup
  31. var container = (element:Dynamic).spectrum("container");
  32. var timer = new haxe.Timer(1000);
  33. timer.run = function() {
  34. if( root.parents("body").length == 0 ) {
  35. container.remove();
  36. timer.stop();
  37. }
  38. };
  39. }
  40. public function open() {
  41. (element:Dynamic).spectrum("show");
  42. }
  43. public function close() {
  44. (element:Dynamic).spectrum("hide");
  45. }
  46. public dynamic function onOpen() {
  47. }
  48. public dynamic function onClose() {
  49. }
  50. public dynamic function onChange(dragging) {
  51. }
  52. function get_value() return innerValue;
  53. function set_value(v) {
  54. v &= mask;
  55. if( innerValue == v )
  56. return v;
  57. (element:Dynamic).spectrum("set", "#"+StringTools.hex(v,mask < 0 ? 8 : 6));
  58. return innerValue = v;
  59. }
  60. }