001 package com.croftsoft.apps.color; 002 003 import java.applet.Applet; 004 import java.awt.*; 005 006 import com.croftsoft.core.lang.lifecycle.Lifecycle; 007 008 /********************************************************************* 009 * Displays colors with RGB and HSB values. 010 * 011 * @version 012 * 2002-02-27 013 * @since 014 * 1996-08-22 015 * @author 016 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 017 *********************************************************************/ 018 019 public class ColorTest 020 extends Applet 021 implements Lifecycle 022 ////////////////////////////////////////////////////////////////////// 023 ////////////////////////////////////////////////////////////////////// 024 { 025 026 Canvas swatch; 027 ColorControls rgbControls, hsbControls; 028 029 public void init ( ) { 030 ////////////////////////////////////////////////////////////////////// 031 Color theColor = new Color ( 0, 0, 0 ); 032 float [ ] hsb = Color.RGBtoHSB ( theColor.getRed ( ), 033 theColor.getGreen ( ), theColor.getBlue ( ), 034 ( new float [ 3 ] ) ); 035 setLayout ( new GridLayout ( 1, 3, 10, 10 ) ); 036 swatch = new Canvas ( ); 037 swatch.setBackground ( theColor ); 038 rgbControls = new ColorControls ( this, 039 "Red (0-255)", "Green (0-255)", "Blue (0-255)", 040 theColor.getRed ( ), theColor.getGreen ( ), 041 theColor.getBlue ( ) ); 042 hsbControls = new ColorControls ( this, 043 "Hue (0-360)", "Saturation (0-100)", "Brightness (0-100)", 044 ( int ) ( hsb [ 0 ] * 360 ), 045 ( int ) ( hsb [ 1 ] * 100 ), 046 ( int ) ( hsb [ 2 ] * 100 ) ); 047 add ( swatch ); 048 add ( rgbControls ); 049 add ( hsbControls ); 050 } 051 052 public Insets insets ( ) { 053 ////////////////////////////////////////////////////////////////////// 054 return new Insets ( 5, 5, 5, 5 ); 055 } 056 057 void update ( ColorControls in ) { 058 ////////////////////////////////////////////////////////////////////// 059 Color c; 060 String v1 = in.f1.getText ( ); 061 String v2 = in.f2.getText ( ); 062 String v3 = in.f3.getText ( ); 063 ////////////////////////////////////////////////////////////////////// 064 if ( in == rgbControls ) { 065 c = new Color ( 066 Integer.parseInt ( v1 ), 067 Integer.parseInt ( v2 ), 068 Integer.parseInt ( v3 ) ); 069 swatch.setBackground ( c ); 070 float [ ] hsb = Color.RGBtoHSB ( 071 c.getRed ( ), c.getGreen ( ), c.getBlue ( ), 072 ( new float [ 3 ] ) ); 073 hsb [ 0 ] *= 360; 074 hsb [ 1 ] *= 100; 075 hsb [ 2 ] *= 100; 076 hsbControls.f1.setText ( String.valueOf ( ( int ) hsb [ 0 ] ) ); 077 hsbControls.f2.setText ( String.valueOf ( ( int ) hsb [ 1 ] ) ); 078 hsbControls.f3.setText ( String.valueOf ( ( int ) hsb [ 2 ] ) ); 079 } else { 080 int f1 = Integer.parseInt ( v1 ); 081 int f2 = Integer.parseInt ( v2 ); 082 int f3 = Integer.parseInt ( v3 ); 083 c = Color.getHSBColor ( 084 ( float ) f1 / 360, 085 ( float ) f2 / 100, 086 ( float ) f3 / 100 ); 087 swatch.setBackground ( c ); 088 rgbControls.f1.setText ( String.valueOf ( c.getRed ( ) ) ); 089 rgbControls.f2.setText ( String.valueOf ( c.getGreen ( ) ) ); 090 rgbControls.f3.setText ( String.valueOf ( c.getBlue ( ) ) ); 091 } 092 } 093 094 ////////////////////////////////////////////////////////////////////// 095 ////////////////////////////////////////////////////////////////////// 096 } 097 098 class ColorControls extends Panel { 099 ////////////////////////////////////////////////////////////////////// 100 ////////////////////////////////////////////////////////////////////// 101 ColorTest outerparent; 102 TextField f1, f2, f3; 103 104 ColorControls ( 105 ColorTest target, 106 String l1, 107 String l2, 108 String l3, 109 int v1, 110 int v2, 111 int v3 ) { 112 ////////////////////////////////////////////////////////////////////// 113 // Constructor method 114 ////////////////////////////////////////////////////////////////////// 115 this.outerparent = target; 116 setLayout ( new GridLayout ( 3, 2, 10, 10 ) ); 117 f1 = new TextField ( String.valueOf ( v1 ), 10 ); 118 f2 = new TextField ( String.valueOf ( v2 ), 10 ); 119 f3 = new TextField ( String.valueOf ( v3 ), 10 ); 120 add ( new Label ( l1, Label.RIGHT ) ); 121 add ( f1 ); 122 add ( new Label ( l2, Label.RIGHT ) ); 123 add ( f2 ); 124 add ( new Label ( l3, Label.RIGHT ) ); 125 add ( f3 ); 126 } 127 128 public boolean action ( Event evt, Object arg ) { 129 ////////////////////////////////////////////////////////////////////// 130 if ( evt.target instanceof TextField ) { 131 outerparent.update ( this ); 132 outerparent.repaint ( ); 133 return true; 134 } else return false; 135 } 136 137 public Insets insets ( ) { 138 ////////////////////////////////////////////////////////////////////// 139 return new Insets ( 5, 5, 0, 0 ); 140 } 141 142 ////////////////////////////////////////////////////////////////////// 143 ////////////////////////////////////////////////////////////////////// 144 } 145