ColorPickerElement.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module INSPECTOR {
  2. /**
  3. * Represents a html div element.
  4. * The div is built when an instance of BasicElement is created.
  5. */
  6. export class ColorPickerElement extends BasicElement {
  7. protected _input : HTMLInputElement;
  8. private pline : PropertyLine;
  9. constructor(color:BABYLON.Color4|BABYLON.Color3, propertyLine: PropertyLine) {
  10. super();
  11. let scheduler = Scheduler.getInstance();
  12. this._div.className = 'color-element';
  13. this._div.style.backgroundColor = this._toRgba(color);
  14. this._div.style.top = "5px";
  15. this.pline = propertyLine;
  16. this._input = Helpers.CreateInput();
  17. this._input.type = 'color';
  18. this._input.style.opacity = "0";
  19. this._input.style.width = '10px';
  20. this._input.style.height = '15px';
  21. this._input.value = color.toHexString();
  22. this._input.addEventListener('input', (e) => {
  23. console.log('Color', this._input.value, this.pline)
  24. this.pline.validateInput(BABYLON.Color3.FromHexString(this._input.value));
  25. scheduler.pause = false;
  26. });
  27. this._div.appendChild(this._input);
  28. this._input.addEventListener('click', (e) => {
  29. scheduler.pause = true;
  30. });
  31. }
  32. public update(color?:BABYLON.Color4|BABYLON.Color3) {
  33. if (color) {
  34. this._div.style.backgroundColor = this._toRgba(color);
  35. this._input.value = color.toHexString();
  36. }
  37. }
  38. private _toRgba(color:BABYLON.Color4|BABYLON.Color3) : string {
  39. if (color) {
  40. let r = (color.r * 255) | 0;
  41. let g = (color.g * 255) | 0;
  42. let b = (color.b * 255) | 0;
  43. let a = 1;
  44. if (color instanceof BABYLON.Color4) {
  45. let a = (color as BABYLON.Color4).a;
  46. }
  47. return `rgba(${r}, ${g}, ${b}, ${a})`;
  48. } else {
  49. return '';
  50. }
  51. }
  52. }
  53. }