ColorPicker.hx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 timer = new haxe.Timer(1000);
  32. timer.run = function() {
  33. if( root.parents("body").length == 0 ) {
  34. (element:Dynamic).spectrum("destroy");
  35. timer.stop();
  36. }
  37. };
  38. }
  39. public function open() {
  40. (element:Dynamic).spectrum("show");
  41. }
  42. public function close() {
  43. (element:Dynamic).spectrum("hide");
  44. }
  45. public dynamic function onOpen() {
  46. }
  47. public dynamic function onClose() {
  48. }
  49. public dynamic function onChange(dragging) {
  50. }
  51. function get_value() return innerValue;
  52. function set_value(v) {
  53. v &= mask;
  54. if( innerValue == v )
  55. return v;
  56. (element:Dynamic).spectrum("set", "#"+StringTools.hex(v,mask < 0 ? 8 : 6));
  57. return innerValue = v;
  58. }
  59. }