/** * Set the Hue, Saturation and Value (Brightness) variables. This will automatically populate the * Red, Green, Blue, and Hexadecimal fields, too. * * <p>HSV represents points in the RGB color space, which attempt to describe perceptual color * relationships more accurately than RGB. HSV describes colors as points in a cylinder whose * central axis ranges from black at the bottom to white at the top with neutral colors between * them, where angle around the axis corresponds to “hue”, distance from the axis corresponds to * “saturation”, and distance along the axis corresponds to “lightness”, “value”, or “brightness”. * * @param hue angle - valid range is 0-359 * @param saturation percent - valid range is 0-100 * @param value percent (Brightness) - valid range is 0-100 * @throws java.lang.Exception A general exception if the Hue, Saturation, or Value variables are * out of range. */ public void setHSV(int hue, int saturation, int value) throws Exception { Color color = new Color(); color.setHSV(hue, saturation, value); this.red = color.getRed(); this.green = color.getGreen(); this.blue = color.getBlue(); this.hue = hue; this.saturation = saturation; this.brightness = value; tbRed.setText(Integer.toString(this.red)); tbGreen.setText(Integer.toString(this.green)); tbBlue.setText(Integer.toString(this.blue)); tbHue.setText(Integer.toString(this.hue)); tbSaturation.setText(Integer.toString(this.saturation)); tbBrightness.setText(Integer.toString(this.brightness)); tbHexColor.setText(color.getHex()); setPreview(color.getHex()); updateSliders(); }
/** * Fired when the user clicks on a widget. * * <p>Subclasses that override this method must call <tt>super.onClick(sender)</tt> to ensure that * the Widget recieves its events. * * @param sender the widget sending the event. */ public void onClick(Widget sender) { if (sender == rbHue) { if (colorMode != SliderMap.Hue) { colorMode = SliderMap.Hue; slidermap.setColorSelectMode(SliderMap.Hue); sliderbar.setColorSelectMode(SliderBar.Hue); slidermap.setOverlayOpacity(100); sliderbar.setLayerOpacity(100, SliderBar.BarD); } try { Color color = new Color(); color.setHSV(hue, 100, 100); slidermap.setOverlayColor("#" + color.getHex()); } catch (Exception e) { } sliderbar.setSliderPosition(256 - (int) ((new Integer(hue).floatValue() / 360) * 256)); slidermap.setSliderPosition( (int) ((new Integer(saturation).floatValue() / 100) * 256), 256 - (int) ((new Integer(brightness).floatValue() / 100) * 256)); } else if (sender == rbSaturation) { if (colorMode != SliderMap.Saturation) { colorMode = SliderMap.Saturation; slidermap.setColorSelectMode(SliderMap.Saturation); sliderbar.setColorSelectMode(SliderBar.Saturation); slidermap.setOverlayColor("transparent"); sliderbar.setLayerColor("#ffffff", SliderBar.BarC); sliderbar.setLayerOpacity(100, SliderBar.BarD); } slidermap.setOverlayOpacity(100 - saturation); sliderbar.setLayerColor(tbHexColor.getText(), SliderBar.BarD); sliderbar.setSliderPosition(256 - (int) ((new Integer(saturation).floatValue() / 100) * 256)); slidermap.setSliderPosition( (int) ((new Integer(hue).floatValue() / 360) * 256), 256 - (int) ((new Integer(brightness).floatValue() / 100) * 256)); } else if (sender == rbBrightness) { if (colorMode != SliderMap.Brightness) { colorMode = SliderMap.Brightness; slidermap.setColorSelectMode(SliderMap.Brightness); sliderbar.setColorSelectMode(SliderBar.Brightness); slidermap.setUnderlayColor("#000000"); slidermap.setOverlayColor("transparent"); sliderbar.setLayerOpacity(100, SliderBar.BarD); } try { Color color = new Color(); color.setHSV(hue, saturation, 100); sliderbar.setLayerColor("#" + color.getHex(), SliderBar.BarD); } catch (Exception e) { } slidermap.setOverlayOpacity(brightness); sliderbar.setSliderPosition(256 - (int) ((new Integer(brightness).floatValue() / 100) * 256)); slidermap.setSliderPosition( (int) ((new Integer(hue).floatValue() / 360) * 256), 256 - (int) ((new Integer(saturation).floatValue() / 100) * 256)); } else if (sender == rbRed) { if (colorMode != SliderMap.Red) { colorMode = SliderMap.Red; slidermap.setColorSelectMode(SliderMap.Red); sliderbar.setColorSelectMode(SliderBar.Red); } slidermap.setOverlayOpacity(percentOf(red, 100)); sliderbar.setSliderPosition(256 - red); slidermap.setSliderPosition(blue, 256 - green); } else if (sender == rbGreen) { if (colorMode != SliderMap.Green) { colorMode = SliderMap.Green; slidermap.setColorSelectMode(SliderMap.Green); sliderbar.setColorSelectMode(SliderBar.Green); } slidermap.setOverlayOpacity(percentOf(green, 100)); sliderbar.setSliderPosition(256 - green); slidermap.setSliderPosition(blue, 256 - red); } else if (sender == rbBlue) { if (colorMode != SliderMap.Blue) { colorMode = SliderMap.Blue; slidermap.setColorSelectMode(SliderMap.Blue); sliderbar.setColorSelectMode(SliderBar.Blue); } slidermap.setOverlayOpacity(percentOf(blue, 100)); sliderbar.setSliderPosition(256 - blue); slidermap.setSliderPosition(red, 256 - green); } if (colorMode == SliderMap.Red || colorMode == SliderMap.Green || colorMode == SliderMap.Blue) { int x = 0; int y = 0; if (colorMode == SliderMap.Red) { x = blue; y = green; } if (colorMode == SliderMap.Green) { x = blue; y = red; } if (colorMode == SliderMap.Blue) { x = red; y = green; } int horzPer = (int) ((new Float(x).floatValue() / 256) * 100); int vertPer = (int) ((new Float(y).floatValue() / 256) * 100); int horzPerRev = (int) (((256 - new Float(x).floatValue()) / 256) * 100); int vertPerRev = (int) (((256 - new Float(y).floatValue()) / 256) * 100); if (vertPerRev > horzPerRev) sliderbar.setLayerOpacity(horzPerRev, SliderBar.BarD); else sliderbar.setLayerOpacity(vertPerRev, SliderBar.BarD); if (vertPerRev > horzPer) sliderbar.setLayerOpacity(horzPer, SliderBar.BarC); else sliderbar.setLayerOpacity(vertPerRev, SliderBar.BarC); if (vertPer > horzPer) sliderbar.setLayerOpacity(horzPer, SliderBar.BarB); else sliderbar.setLayerOpacity(vertPer, SliderBar.BarB); if (vertPer > horzPerRev) sliderbar.setLayerOpacity(horzPerRev, SliderBar.BarA); else sliderbar.setLayerOpacity(vertPer, SliderBar.BarA); } }
/** * Fired whenever something in this widget changes. * * <p>Subclasses that override this method must call <tt>super.onChange(sender)</tt> to ensure * that the Widget recieves its events. * * @param sender the widget that has changed. */ public void onChange(Widget sender) { if (sender == tbHexColor) { // Figure out colors // Color class will do bounds check on hex input try { Color color = new Color(); color.setHex(tbHexColor.getText()); tbHue.setText(Integer.toString(color.getHue())); tbSaturation.setText(Integer.toString(color.getSaturation())); tbBrightness.setText(Integer.toString(color.getValue())); tbRed.setText(Integer.toString(color.getRed())); tbGreen.setText(Integer.toString(color.getGreen())); tbBlue.setText(Integer.toString(color.getBlue())); tbHexColor.setText(color.getHex()); setPreview(color.getHex()); } catch (Exception e) { } } if (sender == tbRed || sender == tbGreen || sender == tbBlue) { // Don't allow this value to overflow or underflow try { if (Integer.parseInt(((TextBox) sender).getText()) > 255) { ((TextBox) sender).setText("255"); } if (Integer.parseInt(((TextBox) sender).getText()) < 0) { ((TextBox) sender).setText("0"); } } catch (Exception e) { } red = Integer.parseInt(tbRed.getText()); green = Integer.parseInt(tbGreen.getText()); blue = Integer.parseInt(tbBlue.getText()); hue = Integer.parseInt(tbHue.getText()); saturation = Integer.parseInt(tbSaturation.getText()); brightness = Integer.parseInt(tbBrightness.getText()); // Figure out the colors try { Color color = new Color(); color.setRGB(red, green, blue); tbHue.setText(Integer.toString(color.getHue())); tbSaturation.setText(Integer.toString(color.getSaturation())); tbBrightness.setText(Integer.toString(color.getValue())); tbHexColor.setText(color.getHex()); setPreview(color.getHex()); } catch (Exception e) { } } else if (sender == tbHue || sender == tbSaturation || sender == tbBrightness) { // Don't allow this value to overflow try { if (Integer.parseInt(tbHue.getText()) > 359) { tbHue.setText("359"); } if (Integer.parseInt(tbSaturation.getText()) > 100) { tbSaturation.setText("100"); } if (Integer.parseInt(tbBrightness.getText()) > 100) { tbBrightness.setText("100"); } } catch (Exception e) { } red = Integer.parseInt(tbRed.getText()); green = Integer.parseInt(tbGreen.getText()); blue = Integer.parseInt(tbBlue.getText()); hue = Integer.parseInt(tbHue.getText()); saturation = Integer.parseInt(tbSaturation.getText()); brightness = Integer.parseInt(tbBrightness.getText()); // Figure out colors try { Color color = new Color(); color.setHSV(hue, saturation, brightness); tbRed.setText(Integer.toString(color.getRed())); tbGreen.setText(Integer.toString(color.getGreen())); tbBlue.setText(Integer.toString(color.getBlue())); tbHexColor.setText(color.getHex()); setPreview(color.getHex()); } catch (Exception e) { } } // Let the sliders know something's changed updateSliders(); ChangeEvent.fireNativeEvent(Document.get().createChangeEvent(), this); }