2
0

ColorEditor.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { ColorInput, StringInput, NumberInput, LabelElement, Element } from 'flow';
  2. import { BaseNodeEditor } from '../BaseNodeEditor.js';
  3. import { Color } from 'three';
  4. import { UniformNode } from 'three/nodes';
  5. export class ColorEditor extends BaseNodeEditor {
  6. constructor() {
  7. const v = new Color();
  8. const node = new UniformNode( v );
  9. super( 'Color', node );
  10. this.setOutputLength( 3 );
  11. const updateFields = ( editing ) => {
  12. const value = node.value;
  13. const hexValue = value.getHex();
  14. const hexString = hexValue.toString( 16 ).toUpperCase().padStart( 6, '0' );
  15. if ( editing !== 'color' ) {
  16. field.setValue( hexValue, false );
  17. }
  18. if ( editing !== 'hex' ) {
  19. hexField.setValue( '#' + hexString, false );
  20. }
  21. if ( editing !== 'rgb' ) {
  22. fieldR.setValue( value.r.toFixed( 3 ), false );
  23. fieldG.setValue( value.g.toFixed( 3 ), false );
  24. fieldB.setValue( value.b.toFixed( 3 ), false );
  25. }
  26. fieldR.setTagColor( `#${hexString.slice( 0, 2 )}0000` );
  27. fieldG.setTagColor( `#00${hexString.slice( 2, 4 )}00` );
  28. fieldB.setTagColor( `#0000${hexString.slice( 4, 6 )}` );
  29. this.invalidate(); // it's important to scriptable nodes ( cpu nodes needs update )
  30. };
  31. const field = new ColorInput( 0xFFFFFF ).onChange( () => {
  32. node.value.setHex( field.getValue() );
  33. updateFields( 'picker' );
  34. } );
  35. const hexField = new StringInput().onChange( () => {
  36. const value = hexField.getValue();
  37. if ( value.indexOf( '#' ) === 0 ) {
  38. const hexStr = value.slice( 1 ).padEnd( 6, '0' );
  39. node.value.setHex( parseInt( hexStr, 16 ) );
  40. updateFields( 'hex' );
  41. }
  42. } );
  43. hexField.addEventListener( 'blur', () => {
  44. updateFields();
  45. } );
  46. const onChangeRGB = () => {
  47. node.value.setRGB( fieldR.getValue(), fieldG.getValue(), fieldB.getValue() );
  48. updateFields( 'rgb' );
  49. };
  50. const fieldR = new NumberInput( 1, 0, 1 ).setTagColor( 'red' ).onChange( onChangeRGB );
  51. const fieldG = new NumberInput( 1, 0, 1 ).setTagColor( 'green' ).onChange( onChangeRGB );
  52. const fieldB = new NumberInput( 1, 0, 1 ).setTagColor( 'blue' ).onChange( onChangeRGB );
  53. this.add( new Element().add( field ).setSerializable( false ) )
  54. .add( new LabelElement( 'Hex' ).add( hexField ).setSerializable( false ) )
  55. .add( new LabelElement( 'RGB' ).add( fieldR ).add( fieldG ).add( fieldB ) );
  56. updateFields();
  57. }
  58. }