1. /*
  2. * @(#)DefaultHSBChooserPanel.java 1.22 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.colorchooser;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.event.*;
  12. import javax.swing.border.*;
  13. import java.awt.image.*;
  14. /**
  15. * Implements the default HSB Color chooser
  16. *
  17. * @version 1.22 01/23/03
  18. * @author Tom Santos
  19. * @author Steve Wilson
  20. * @author Mark Davidson
  21. * @author Shannon Hickey
  22. */
  23. class DefaultHSBChooserPanel extends AbstractColorChooserPanel implements ChangeListener, HierarchyListener {
  24. private HSBImage palette;
  25. private HSBImage sliderPalette;
  26. private Image paletteImage;
  27. private Image sliderPaletteImage;
  28. private JSlider slider;
  29. private JSpinner hField;
  30. private JSpinner sField;
  31. private JSpinner bField;
  32. private JTextField redField;
  33. private JTextField greenField;
  34. private JTextField blueField;
  35. private boolean isAdjusting = false; // Flag which indicates that values are set internally
  36. private Point paletteSelection = new Point();
  37. private JLabel paletteLabel;
  38. private JLabel sliderPaletteLabel;
  39. private JRadioButton hRadio;
  40. private JRadioButton sRadio;
  41. private JRadioButton bRadio;
  42. private static final int PALETTE_DIMENSION = 200;
  43. private static final int MAX_HUE_VALUE = 359;
  44. private static final int MAX_SATURATION_VALUE = 100;
  45. private static final int MAX_BRIGHTNESS_VALUE = 100;
  46. private int currentMode = HUE_MODE;
  47. private static final int HUE_MODE = 0;
  48. private static final int SATURATION_MODE = 1;
  49. private static final int BRIGHTNESS_MODE = 2;
  50. public DefaultHSBChooserPanel() {
  51. }
  52. private void addPaletteListeners() {
  53. paletteLabel.addMouseListener(new MouseAdapter() {
  54. public void mousePressed(MouseEvent e ) {
  55. float[] hsb = new float[3];
  56. palette.getHSBForLocation( e.getX(), e.getY(), hsb );
  57. updateHSB( hsb[0], hsb[1], hsb[2] );
  58. }
  59. });
  60. paletteLabel.addMouseMotionListener(new MouseMotionAdapter() {
  61. public void mouseDragged( MouseEvent e ){
  62. int labelWidth = paletteLabel.getWidth();
  63. int labelHeight = paletteLabel.getHeight();
  64. int x = e.getX();
  65. int y = e.getY();
  66. if ( x >= labelWidth ) {
  67. x = labelWidth - 1;
  68. }
  69. if ( y >= labelHeight ) {
  70. y = labelHeight - 1;
  71. }
  72. if ( x < 0 ) {
  73. x = 0;
  74. }
  75. if ( y < 0 ) {
  76. y = 0;
  77. }
  78. float[] hsb = new float[3];
  79. palette.getHSBForLocation( x, y, hsb );
  80. updateHSB( hsb[0], hsb[1], hsb[2] );
  81. }
  82. });
  83. }
  84. private void updatePalette( float h, float s, float b ) {
  85. int x = 0;
  86. int y = 0;
  87. switch ( currentMode ) {
  88. case HUE_MODE:
  89. if ( h != palette.getHue() ) {
  90. palette.setHue( h );
  91. palette.nextFrame();
  92. }
  93. x = PALETTE_DIMENSION - (int)(s * PALETTE_DIMENSION);
  94. y = PALETTE_DIMENSION - (int)(b * PALETTE_DIMENSION);
  95. break;
  96. case SATURATION_MODE:
  97. if ( s != palette.getSaturation() ) {
  98. palette.setSaturation( s );
  99. palette.nextFrame();
  100. }
  101. x = (int)(h * PALETTE_DIMENSION);
  102. y = PALETTE_DIMENSION - (int)(b * PALETTE_DIMENSION);
  103. break;
  104. case BRIGHTNESS_MODE:
  105. if ( b != palette.getBrightness() ) {
  106. palette.setBrightness( b );
  107. palette.nextFrame();
  108. }
  109. x = (int)(h * PALETTE_DIMENSION);
  110. y = PALETTE_DIMENSION - (int)(s * PALETTE_DIMENSION);
  111. break;
  112. }
  113. paletteSelection.setLocation( x, y );
  114. paletteLabel.repaint();
  115. }
  116. private void updateSlider( float h, float s, float b ) {
  117. // Update the slider palette if necessary.
  118. // When the slider is the hue slider or the hue hasn't changed,
  119. // the hue of the palette will not need to be updated.
  120. if (currentMode != HUE_MODE && h != sliderPalette.getHue() ) {
  121. sliderPalette.setHue( h );
  122. sliderPalette.nextFrame();
  123. }
  124. float value = 0f;
  125. switch ( currentMode ) {
  126. case HUE_MODE:
  127. value = h;
  128. break;
  129. case SATURATION_MODE:
  130. value = s;
  131. break;
  132. case BRIGHTNESS_MODE:
  133. value = b;
  134. break;
  135. }
  136. slider.setValue( Math.round(value * (slider.getMaximum())) );
  137. }
  138. private void updateHSBTextFields( float hue, float saturation, float brightness ) {
  139. int h = Math.round(hue * 359);
  140. int s = Math.round(saturation * 100);
  141. int b = Math.round(brightness * 100);
  142. if (((Integer)hField.getValue()).intValue() != h) {
  143. hField.setValue(new Integer(h));
  144. }
  145. if (((Integer)sField.getValue()).intValue() != s) {
  146. sField.setValue(new Integer(s));
  147. }
  148. if (((Integer)bField.getValue()).intValue() != b) {
  149. bField.setValue(new Integer(b));
  150. }
  151. }
  152. /**
  153. * Updates the values of the RGB fields to reflect the new color change
  154. */
  155. private void updateRGBTextFields( Color color ) {
  156. redField.setText(String.valueOf(color.getRed()));
  157. greenField.setText(String.valueOf(color.getGreen()));
  158. blueField.setText(String.valueOf(color.getBlue()));
  159. }
  160. /**
  161. * Main internal method of updating the ui controls and the color model.
  162. */
  163. private void updateHSB( float h, float s, float b ) {
  164. if ( !isAdjusting ) {
  165. isAdjusting = true;
  166. updatePalette( h, s, b );
  167. updateSlider( h, s, b );
  168. updateHSBTextFields( h, s, b );
  169. Color color = Color.getHSBColor(h, s, b);
  170. updateRGBTextFields( color );
  171. getColorSelectionModel().setSelectedColor( color );
  172. isAdjusting = false;
  173. }
  174. }
  175. /**
  176. * Invoked automatically when the model's state changes.
  177. * It is also called by <code>installChooserPanel</code> to allow
  178. * you to set up the initial state of your chooser.
  179. * Override this method to update your <code>ChooserPanel</code>.
  180. */
  181. public void updateChooser() {
  182. if ( !isAdjusting ) {
  183. float[] hsb = getHSBColorFromModel();
  184. updateHSB( hsb[0], hsb[1], hsb[2] );
  185. }
  186. }
  187. public void installChooserPanel(JColorChooser enclosingChooser) {
  188. super.installChooserPanel(enclosingChooser);
  189. addHierarchyListener(this);
  190. }
  191. /**
  192. * Invoked when the panel is removed from the chooser.
  193. */
  194. public void uninstallChooserPanel(JColorChooser enclosingChooser) {
  195. super.uninstallChooserPanel(enclosingChooser);
  196. cleanupPalettesIfNecessary();
  197. removeAll();
  198. removeHierarchyListener(this);
  199. }
  200. /**
  201. * Returns an float array containing the HSB values of the selected color from
  202. * the ColorSelectionModel
  203. */
  204. private float[] getHSBColorFromModel() {
  205. Color color = getColorFromModel();
  206. float[] hsb = new float[3];
  207. Color.RGBtoHSB( color.getRed(), color.getGreen(), color.getBlue(), hsb );
  208. return hsb;
  209. }
  210. /**
  211. * Builds a new chooser panel.
  212. */
  213. protected void buildChooser() {
  214. setLayout(new BorderLayout());
  215. JComponent spp = buildSliderPalettePanel();
  216. add(spp, BorderLayout.BEFORE_LINE_BEGINS);
  217. JPanel controlHolder = new JPanel(new SmartGridLayout(1,3));
  218. JComponent hsbControls = buildHSBControls();
  219. controlHolder.add(hsbControls);
  220. controlHolder.add(new JLabel(" ")); // spacer
  221. JComponent rgbControls = buildRGBControls();
  222. controlHolder.add(rgbControls);
  223. controlHolder.setBorder(new EmptyBorder( 10, 5, 10, 5));
  224. add( controlHolder, BorderLayout.CENTER);
  225. }
  226. /**
  227. * Creates the panel with the uneditable RGB field
  228. */
  229. private JComponent buildRGBControls() {
  230. JPanel panel = new JPanel(new SmartGridLayout(2,3));
  231. Color color = getColorFromModel();
  232. redField = new JTextField( String.valueOf(color.getRed()), 3 );
  233. redField.setEditable(false);
  234. redField.setHorizontalAlignment( JTextField.RIGHT );
  235. greenField = new JTextField(String.valueOf(color.getGreen()), 3 );
  236. greenField.setEditable(false);
  237. greenField.setHorizontalAlignment( JTextField.RIGHT );
  238. blueField = new JTextField( String.valueOf(color.getBlue()), 3 );
  239. blueField.setEditable(false);
  240. blueField.setHorizontalAlignment( JTextField.RIGHT );
  241. String redString = UIManager.getString("ColorChooser.hsbRedText");
  242. String greenString = UIManager.getString("ColorChooser.hsbGreenText");
  243. String blueString = UIManager.getString("ColorChooser.hsbBlueText");
  244. panel.add( new JLabel(redString) );
  245. panel.add( redField );
  246. panel.add( new JLabel(greenString) );
  247. panel.add( greenField );
  248. panel.add( new JLabel(blueString) );
  249. panel.add( blueField );
  250. return panel;
  251. }
  252. /**
  253. * Creates the panel with the editable HSB fields and the radio buttons.
  254. */
  255. private JComponent buildHSBControls() {
  256. String hueString = UIManager.getString("ColorChooser.hsbHueText");
  257. String saturationString = UIManager.getString("ColorChooser.hsbSaturationText");
  258. String brightnessString = UIManager.getString("ColorChooser.hsbBrightnessText");
  259. RadioButtonHandler handler = new RadioButtonHandler();
  260. hRadio = new JRadioButton(hueString);
  261. hRadio.addActionListener(handler);
  262. hRadio.setSelected(true);
  263. sRadio = new JRadioButton(saturationString);
  264. sRadio.addActionListener(handler);
  265. bRadio = new JRadioButton(brightnessString);
  266. bRadio.addActionListener(handler);
  267. ButtonGroup group = new ButtonGroup();
  268. group.add(hRadio);
  269. group.add(sRadio);
  270. group.add(bRadio);
  271. float[] hsb = getHSBColorFromModel();
  272. hField = new JSpinner(new SpinnerNumberModel((int)(hsb[0] * 359), 0, 359, 1));
  273. sField = new JSpinner(new SpinnerNumberModel((int)(hsb[1] * 100), 0, 100, 1));
  274. bField = new JSpinner(new SpinnerNumberModel((int)(hsb[2] * 100), 0, 100, 1));
  275. hField.addChangeListener(this);
  276. sField.addChangeListener(this);
  277. bField.addChangeListener(this);
  278. JPanel panel = new JPanel( new SmartGridLayout(2, 3) );
  279. panel.add(hRadio);
  280. panel.add(hField);
  281. panel.add(sRadio);
  282. panel.add(sField);
  283. panel.add(bRadio);
  284. panel.add(bField);
  285. return panel;
  286. }
  287. /**
  288. * Handler for the radio button classes.
  289. */
  290. private class RadioButtonHandler implements ActionListener {
  291. public void actionPerformed(ActionEvent evt) {
  292. Object obj = evt.getSource();
  293. if (obj instanceof JRadioButton) {
  294. JRadioButton button = (JRadioButton)obj;
  295. if (button == hRadio) {
  296. setMode(HUE_MODE);
  297. } else if (button == sRadio) {
  298. setMode(SATURATION_MODE);
  299. } else if (button == bRadio) {
  300. setMode(BRIGHTNESS_MODE);
  301. }
  302. }
  303. }
  304. }
  305. private void setMode(int mode) {
  306. if (currentMode == mode) {
  307. return;
  308. }
  309. isAdjusting = true; // Ensure no events propagate from changing slider value.
  310. currentMode = mode;
  311. float[] hsb = getHSBColorFromModel();
  312. switch (currentMode) {
  313. case HUE_MODE:
  314. slider.setInverted(true);
  315. slider.setMaximum(MAX_HUE_VALUE);
  316. palette.setValues(HSBImage.HSQUARE, hsb[0], 1.0f, 1.0f);
  317. sliderPalette.setValues(HSBImage.HSLIDER, 0f, 1.0f, 1.0f);
  318. break;
  319. case SATURATION_MODE:
  320. slider.setInverted(false);
  321. slider.setMaximum(MAX_SATURATION_VALUE);
  322. palette.setValues(HSBImage.SSQUARE, hsb[0], hsb[1], 1.0f);
  323. sliderPalette.setValues(HSBImage.SSLIDER, hsb[0], 1.0f, 1.0f);
  324. break;
  325. case BRIGHTNESS_MODE:
  326. slider.setInverted(false);
  327. slider.setMaximum(MAX_BRIGHTNESS_VALUE);
  328. palette.setValues(HSBImage.BSQUARE, hsb[0], 1.0f, hsb[2]);
  329. sliderPalette.setValues(HSBImage.BSLIDER, hsb[0], 1.0f, 1.0f);
  330. break;
  331. }
  332. isAdjusting = false;
  333. palette.nextFrame();
  334. sliderPalette.nextFrame();
  335. updateChooser();
  336. }
  337. protected JComponent buildSliderPalettePanel() {
  338. // This slider has to have a minimum of 0. A lot of math in this file is simplified due to this.
  339. slider = new JSlider(JSlider.VERTICAL, 0, MAX_HUE_VALUE, 0);
  340. slider.setInverted(true);
  341. slider.setPaintTrack(false);
  342. slider.setPreferredSize(new Dimension(slider.getPreferredSize().width, PALETTE_DIMENSION + 15));
  343. slider.addChangeListener(this);
  344. paletteLabel = createPaletteLabel();
  345. addPaletteListeners();
  346. sliderPaletteLabel = new JLabel();
  347. JPanel panel = new JPanel();
  348. panel.add( paletteLabel );
  349. panel.add( slider );
  350. panel.add( sliderPaletteLabel );
  351. initializePalettesIfNecessary();
  352. return panel;
  353. }
  354. private void initializePalettesIfNecessary() {
  355. if (palette != null) {
  356. return;
  357. }
  358. float[] hsb = getHSBColorFromModel();
  359. switch(currentMode){
  360. case HUE_MODE:
  361. palette = new HSBImage(HSBImage.HSQUARE, PALETTE_DIMENSION, PALETTE_DIMENSION, hsb[0], 1.0f, 1.0f);
  362. sliderPalette = new HSBImage(HSBImage.HSLIDER, 16, PALETTE_DIMENSION, 0f, 1.0f, 1.0f);
  363. break;
  364. case SATURATION_MODE:
  365. palette = new HSBImage(HSBImage.SSQUARE, PALETTE_DIMENSION, PALETTE_DIMENSION, 1.0f, hsb[1], 1.0f);
  366. sliderPalette = new HSBImage(HSBImage.SSLIDER, 16, PALETTE_DIMENSION, 1.0f, 0f, 1.0f);
  367. break;
  368. case BRIGHTNESS_MODE:
  369. palette = new HSBImage(HSBImage.BSQUARE, PALETTE_DIMENSION, PALETTE_DIMENSION, 1.0f, 1.0f, hsb[2]);
  370. sliderPalette = new HSBImage(HSBImage.BSLIDER, 16, PALETTE_DIMENSION, 1.0f, 1.0f, 0f);
  371. break;
  372. }
  373. paletteImage = Toolkit.getDefaultToolkit().createImage(palette);
  374. sliderPaletteImage = Toolkit.getDefaultToolkit().createImage(sliderPalette);
  375. paletteLabel.setIcon(new ImageIcon(paletteImage));
  376. sliderPaletteLabel.setIcon(new ImageIcon(sliderPaletteImage));
  377. }
  378. private void cleanupPalettesIfNecessary() {
  379. if (palette == null) {
  380. return;
  381. }
  382. palette.aborted = true;
  383. sliderPalette.aborted = true;
  384. palette.nextFrame();
  385. sliderPalette.nextFrame();
  386. palette = null;
  387. sliderPalette = null;
  388. paletteImage = null;
  389. sliderPaletteImage = null;
  390. paletteLabel.setIcon(null);
  391. sliderPaletteLabel.setIcon(null);
  392. }
  393. protected JLabel createPaletteLabel() {
  394. return new JLabel() {
  395. protected void paintComponent( Graphics g ) {
  396. super.paintComponent( g );
  397. g.setColor( Color.white );
  398. g.drawOval( paletteSelection.x - 4, paletteSelection.y - 4, 8, 8 );
  399. }
  400. };
  401. }
  402. public String getDisplayName() {
  403. return UIManager.getString("ColorChooser.hsbNameText");
  404. }
  405. /**
  406. * Provides a hint to the look and feel as to the
  407. * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
  408. * access the panel. A return value <= 0 indicates there is no mnemonic.
  409. * <p>
  410. * The return value here is a hint, it is ultimately up to the look
  411. * and feel to honor the return value in some meaningful way.
  412. * <p>
  413. * This implementation looks up the value from the default
  414. * <code>ColorChooser.hsbMnemonic</code>, or if it
  415. * isn't available (or not an <code>Integer</code>) returns -1.
  416. * The lookup for the default is done through the <code>UIManager</code>:
  417. * <code>UIManager.get("ColorChooser.rgbMnemonic");</code>.
  418. *
  419. * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
  420. * mnemonic
  421. * @see #getDisplayedMnemonicIndex
  422. * @since 1.4
  423. */
  424. public int getMnemonic() {
  425. return getInt("ColorChooser.hsbMnemonic", -1);
  426. }
  427. /**
  428. * Provides a hint to the look and feel as to the index of the character in
  429. * <code>getDisplayName</code> that should be visually identified as the
  430. * mnemonic. The look and feel should only use this if
  431. * <code>getMnemonic</code> returns a value > 0.
  432. * <p>
  433. * The return value here is a hint, it is ultimately up to the look
  434. * and feel to honor the return value in some meaningful way. For example,
  435. * a look and feel may wish to render each
  436. * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
  437. * and further use this return value to underline a character in
  438. * the <code>getDisplayName</code>.
  439. * <p>
  440. * This implementation looks up the value from the default
  441. * <code>ColorChooser.rgbDisplayedMnemonicIndex</code>, or if it
  442. * isn't available (or not an <code>Integer</code>) returns -1.
  443. * The lookup for the default is done through the <code>UIManager</code>:
  444. * <code>UIManager.get("ColorChooser.hsbDisplayedMnemonicIndex");</code>.
  445. *
  446. * @return Character index to render mnemonic for; -1 to provide no
  447. * visual identifier for this panel.
  448. * @see #getMnemonic
  449. * @since 1.4
  450. */
  451. public int getDisplayedMnemonicIndex() {
  452. return getInt("ColorChooser.hsbDisplayedMnemonicIndex", -1);
  453. }
  454. public Icon getSmallDisplayIcon() {
  455. return null;
  456. }
  457. public Icon getLargeDisplayIcon() {
  458. return null;
  459. }
  460. /**
  461. * Class for the slider and palette images.
  462. */
  463. class HSBImage extends SyntheticImage {
  464. protected float h = .0f;
  465. protected float s = .0f;
  466. protected float b = .0f;
  467. protected float[] hsb = new float[3];
  468. protected boolean isDirty = true;
  469. protected int cachedY;
  470. protected int cachedColor;
  471. protected int type;
  472. private static final int HSQUARE = 0;
  473. private static final int SSQUARE = 1;
  474. private static final int BSQUARE = 2;
  475. private static final int HSLIDER = 3;
  476. private static final int SSLIDER = 4;
  477. private static final int BSLIDER = 5;
  478. protected HSBImage(int type, int width, int height, float h, float s, float b) {
  479. super(width, height);
  480. setValues(type, h, s, b);
  481. }
  482. public void setValues(int type, float h, float s, float b) {
  483. this.type = type;
  484. cachedY = -1;
  485. cachedColor = 0;
  486. setHue( h );
  487. setSaturation( s );
  488. setBrightness( b );
  489. }
  490. public final void setHue( float hue ) {
  491. h = hue;
  492. }
  493. public final void setSaturation( float saturation ) {
  494. s = saturation;
  495. }
  496. public final void setBrightness( float brightness ) {
  497. b = brightness;
  498. }
  499. public final float getHue() {
  500. return h;
  501. }
  502. public final float getSaturation() {
  503. return s;
  504. }
  505. public final float getBrightness() {
  506. return b;
  507. }
  508. protected boolean isStatic() {
  509. return false;
  510. }
  511. public synchronized void nextFrame() {
  512. isDirty = true;
  513. notifyAll();
  514. }
  515. public synchronized void addConsumer(ImageConsumer ic) {
  516. isDirty = true;
  517. super.addConsumer(ic);
  518. }
  519. private int getRGBForLocation( int x, int y ) {
  520. if (type >= HSLIDER && y == cachedY) {
  521. return cachedColor;
  522. }
  523. getHSBForLocation( x, y, hsb );
  524. cachedY = y;
  525. cachedColor = Color.HSBtoRGB( hsb[0], hsb[1], hsb[2] );
  526. return cachedColor;
  527. }
  528. public void getHSBForLocation( int x, int y, float[] hsbArray ) {
  529. switch (type) {
  530. case HSQUARE: {
  531. float saturationStep = ((float)x) / width;
  532. float brightnessStep = ((float)y) / height;
  533. hsbArray[0] = h;
  534. hsbArray[1] = s - saturationStep;
  535. hsbArray[2] = b - brightnessStep;
  536. break;
  537. }
  538. case SSQUARE: {
  539. float brightnessStep = ((float)y) / height;
  540. float step = 1.0f / ((float)width);
  541. hsbArray[0] = x * step;
  542. hsbArray[1] = s;
  543. hsbArray[2] = 1.0f - brightnessStep;
  544. break;
  545. }
  546. case BSQUARE: {
  547. float saturationStep = ((float)y) / height;
  548. float step = 1.0f / ((float)width);
  549. hsbArray[0] = x * step;
  550. hsbArray[1] = 1.0f - saturationStep;
  551. hsbArray[2] = b;
  552. break;
  553. }
  554. case HSLIDER: {
  555. float step = 1.0f / ((float)height);
  556. hsbArray[0] = y * step;
  557. hsbArray[1] = s;
  558. hsbArray[2] = b;
  559. break;
  560. }
  561. case SSLIDER: {
  562. float saturationStep = ((float)y) / height;
  563. hsbArray[0] = h;
  564. hsbArray[1] = s - saturationStep;
  565. hsbArray[2] = b;
  566. break;
  567. }
  568. case BSLIDER: {
  569. float brightnessStep = ((float)y) / height;
  570. hsbArray[0] = h;
  571. hsbArray[1] = s;
  572. hsbArray[2] = b - brightnessStep;
  573. break;
  574. }
  575. }
  576. }
  577. /**
  578. * Overriden method from SyntheticImage
  579. */
  580. protected void computeRow( int y, int[] row ) {
  581. if ( y == 0 ) {
  582. synchronized ( this ) {
  583. try {
  584. while ( !isDirty ) {
  585. wait();
  586. }
  587. } catch (InterruptedException ie) {
  588. }
  589. isDirty = false;
  590. }
  591. }
  592. if (aborted) {
  593. return;
  594. }
  595. for ( int i = 0; i < row.length; ++i ) {
  596. row[i] = getRGBForLocation( i, y );
  597. }
  598. }
  599. }
  600. public void stateChanged(ChangeEvent e) {
  601. if (e.getSource() == slider) {
  602. boolean modelIsAdjusting = slider.getModel().getValueIsAdjusting();
  603. if (!modelIsAdjusting && !isAdjusting) {
  604. int sliderValue = slider.getValue();
  605. int sliderRange = slider.getMaximum();
  606. float value = (float)sliderValue / (float)sliderRange;
  607. float[] hsb = getHSBColorFromModel();
  608. switch ( currentMode ){
  609. case HUE_MODE:
  610. updateHSB(value, hsb[1], hsb[2]);
  611. break;
  612. case SATURATION_MODE:
  613. updateHSB(hsb[0], value, hsb[2]);
  614. break;
  615. case BRIGHTNESS_MODE:
  616. updateHSB(hsb[0], hsb[1], value);
  617. break;
  618. }
  619. }
  620. } else if (e.getSource() instanceof JSpinner) {
  621. float hue = ((Integer)hField.getValue()).floatValue() / 359f;
  622. float saturation = ((Integer)sField.getValue()).floatValue() / 100f;
  623. float brightness = ((Integer)bField.getValue()).floatValue() / 100f;
  624. updateHSB(hue, saturation, brightness);
  625. }
  626. }
  627. public void hierarchyChanged(HierarchyEvent he) {
  628. if ((he.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
  629. if (isDisplayable()) {
  630. initializePalettesIfNecessary();
  631. } else {
  632. cleanupPalettesIfNecessary();
  633. }
  634. }
  635. }
  636. }