/* * [JColourChooser.java] * * Summary: Incomplete replacement for Sun's JColorChooser. * * Copyright: (c) 2007-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2007-08-15 initial version, just a mockup. No hooks into application. */ package com.mindprod.jcolourchooser; import com.mindprod.common18.FontFactory; import com.mindprod.palette.NamedColor; import javax.swing.BorderFactory; import javax.swing.JComboBox; import javax.swing.JFormattedTextField; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.JSpinner; import javax.swing.JTextArea; import javax.swing.SpinnerNumberModel; import javax.swing.border.TitledBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.Color; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; /** * Incomplete replacement for Sun's JColorChooser. *

* This is a skeleton of a replacement for JColorChooser. *

* advantages: * - displays its results in hex * - lets you see selected colour in real time while you slide. * - lets you select foreground and background colours simultaneously, or * independently. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2007-08-15 Created with IntelliJ IDEA. * @since 2007-08-15 */ public class JColourChooser extends JPanel { /** * true if want debugging harness included */ private static final boolean DEBUGGING = false; private static final int FIRST_COPYRIGHT_YEAR = 2007; /** * width of border around inside cell */ private static final int INNER_BORDER = 2; /** * width of border around cell bordering on frame edge. */ private static final int OUTER_BORDER = 10; /** * extra vertical padding for a JSlider */ private static final int SLIDER_PAD = 255; /** * extra horizontal padding for a JSpinner */ private static final int SPINNER_PAD = 0; /** * undisplayed copyright notice */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2007-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String RELEASE_DATE = "2007-08-15"; /** * text sample, will be configurable in the actual version */ private static final String TEXT_SAMPLE = "The quick brown fox\njumps over the lazy dog's back."; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String TITLE_STRING = "JColourChooser"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String VERSION_STRING = "1.0"; private static final String[] backForeLabels = { "background", "foreground" }; private static final String[] hsbLabels = { "hue", "sat", "bri" }; private static final String[] rgbLabels = { "red", "green", "blue" }; private static final Color BACKGROUND_FOR_HEX = new Color( 0xfff8dc );/* cornsilk */ private static final Color BACKGROUND_FOR_INITIAL = new Color( 0xfff8dc );/* cornsilk */ private static final Color BACKGROUND_FOR_LABEL = new Color( 0xfff8dc );/* cornsilk */ /** * background colour used to indicate hex value */ private static final Color FOREGROUND_FOR_HEX = new Color( 0xffffe0 ); private static final Color FOREGROUND_FOR_INITIAL = new Color( 0x556b2f );/* DarkOliveGreen */ private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 ); private static final Color PALEBLUE = new Color( 0xf0f0ff ); private static final Color PALEGREEN = new Color( 0xf0fff0 ); private static final Color PALERED = new Color( 0xfff0f0 ); private static final Color SLATEBLUE = new Color( 0x6a5acd ); private static final NamedColor[] x11Palette = { new NamedColor( 0xf0f8ff, "AliceBlue" ), new NamedColor( 0xfaebd7, "AntiqueWhite" ), new NamedColor( 0xffefdb, "AntiqueWhite1" ), new NamedColor( 0xeedfcc, "AntiqueWhite2" ), new NamedColor( 0xcdc0b0, "AntiqueWhite3" ), new NamedColor( 0x8b8378, "AntiqueWhite4" ), new NamedColor( 0x7fffd4, "aquamarine" ), new NamedColor( 0x7fffd4, "aquamarine1" ), new NamedColor( 0x76eec6, "aquamarine2" ), new NamedColor( 0x66cdaa, "aquamarine3" ), new NamedColor( 0x458b74, "aquamarine4" ), new NamedColor( 0xf0ffff, "azure" ), new NamedColor( 0xf0ffff, "azure1" ), new NamedColor( 0xe0eeee, "azure2" ), new NamedColor( 0xc1cdcd, "azure3" ), new NamedColor( 0x838b8b, "azure4" ), new NamedColor( 0xf5f5dc, "beige" ), new NamedColor( 0xffe4c4, "bisque" ), new NamedColor( 0xffe4c4, "bisque1" ), new NamedColor( 0xeed5b7, "bisque2" ), new NamedColor( 0xcdb79e, "bisque3" ), new NamedColor( 0x8b7d6b, "bisque4" ), new NamedColor( 0x000000, "black" ), new NamedColor( 0xffebcd, "BlanchedAlmond" ), new NamedColor( 0x0000ff, "blue" ), new NamedColor( 0x0000ff, "blue1" ), new NamedColor( 0x0000ee, "blue2" ), new NamedColor( 0x0000cd, "blue3" ), new NamedColor( 0x00008b, "blue4" ), new NamedColor( 0x8a2be2, "BlueViolet" ), new NamedColor( 0xa52a2a, "brown" ), new NamedColor( 0xff4040, "brown1" ), new NamedColor( 0xee3b3b, "brown2" ), new NamedColor( 0xcd3333, "brown3" ), new NamedColor( 0x8b2323, "brown4" ), new NamedColor( 0xdeb887, "burlywood" ), new NamedColor( 0xffd39b, "burlywood1" ), new NamedColor( 0xeec591, "burlywood2" ), new NamedColor( 0xcdaa7d, "burlywood3" ), new NamedColor( 0x8b7355, "burlywood4" ), new NamedColor( 0x5f9ea0, "CadetBlue" ), new NamedColor( 0x98f5ff, "CadetBlue1" ), new NamedColor( 0x8ee5ee, "CadetBlue2" ), new NamedColor( 0x7ac5cd, "CadetBlue3" ), new NamedColor( 0x53868b, "CadetBlue4" ), new NamedColor( 0x7fff00, "chartreuse" ), new NamedColor( 0x7fff00, "chartreuse1" ), new NamedColor( 0x76ee00, "chartreuse2" ), new NamedColor( 0x66cd00, "chartreuse3" ), new NamedColor( 0x458b00, "chartreuse4" ), new NamedColor( 0xd2691e, "chocolate" ), new NamedColor( 0xff7f24, "chocolate1" ), new NamedColor( 0xee7621, "chocolate2" ), new NamedColor( 0xcd661d, "chocolate3" ), new NamedColor( 0x8b4513, "chocolate4" ), new NamedColor( 0xff7f50, "coral" ), new NamedColor( 0xff7256, "coral1" ), new NamedColor( 0xee6a50, "coral2" ), new NamedColor( 0xcd5b45, "coral3" ), new NamedColor( 0x8b3e2f, "coral4" ), new NamedColor( 0x6495ed, "CornflowerBlue" ), new NamedColor( 0xfff8dc, "cornsilk" ), new NamedColor( 0xfff8dc, "cornsilk1" ), new NamedColor( 0xeee8cd, "cornsilk2" ), new NamedColor( 0xcdc8b1, "cornsilk3" ), new NamedColor( 0x8b8878, "cornsilk4" ), new NamedColor( 0x00ffff, "cyan" ), new NamedColor( 0x00ffff, "cyan1" ), new NamedColor( 0x00eeee, "cyan2" ), new NamedColor( 0x00cdcd, "cyan3" ), new NamedColor( 0x008b8b, "cyan4" ), new NamedColor( 0x00008b, "DarkBlue" ), new NamedColor( 0x008b8b, "DarkCyan" ), new NamedColor( 0xb8860b, "DarkGoldenrod" ), new NamedColor( 0xffb90f, "DarkGoldenrod1" ), new NamedColor( 0xeead0e, "DarkGoldenrod2" ), new NamedColor( 0xcd950c, "DarkGoldenrod3" ), new NamedColor( 0x8b6508, "DarkGoldenrod4" ), new NamedColor( 0xa9a9a9, "DarkGray" ), new NamedColor( 0x006400, "DARK_GREEN" ), new NamedColor( 0xa9a9a9, "DarkGrey" ), new NamedColor( 0xbdb76b, "DarkKhaki" ), new NamedColor( 0x8b008b, "DarkMagenta" ), new NamedColor( 0x556b2f, "DarkOliveGreen" ), new NamedColor( 0xcaff70, "DarkOliveGreen1" ), new NamedColor( 0xbcee68, "DarkOliveGreen2" ), new NamedColor( 0xa2cd5a, "DarkOliveGreen3" ), new NamedColor( 0x6e8b3d, "DarkOliveGreen4" ), new NamedColor( 0xff8c00, "DarkOrange" ), new NamedColor( 0xff7f00, "DarkOrange1" ), new NamedColor( 0xee7600, "DarkOrange2" ), new NamedColor( 0xcd6600, "DarkOrange3" ), new NamedColor( 0x8b4500, "DarkOrange4" ), new NamedColor( 0x9932cc, "DarkOrchid" ), new NamedColor( 0xbf3eff, "DarkOrchid1" ), new NamedColor( 0xb23aee, "DarkOrchid2" ), new NamedColor( 0x9a32cd, "DarkOrchid3" ), new NamedColor( 0x68228b, "DarkOrchid4" ), new NamedColor( 0x8b0000, "DarkRed" ), new NamedColor( 0xe9967a, "DarkSalmon" ), new NamedColor( 0x8fbc8f, "DarkSeaGreen" ), new NamedColor( 0xc1ffc1, "DarkSeaGreen1" ), new NamedColor( 0xb4eeb4, "DarkSeaGreen2" ), new NamedColor( 0x9bcd9b, "DarkSeaGreen3" ), new NamedColor( 0x698b69, "DarkSeaGreen4" ), new NamedColor( 0x483d8b, "DarkSlateBlue" ), new NamedColor( 0x2f4f4f, "DarkSlateGray" ), new NamedColor( 0x97ffff, "DarkSlateGray1" ), new NamedColor( 0x8deeee, "DarkSlateGray2" ), new NamedColor( 0x79cdcd, "DarkSlateGray3" ), new NamedColor( 0x528b8b, "DarkSlateGray4" ), new NamedColor( 0x2f4f4f, "DarkSlateGrey" ), new NamedColor( 0x00ced1, "DarkTurquoise" ), new NamedColor( 0x9400d3, "DarkViolet" ), new NamedColor( 0xff1493, "DeepPink" ), new NamedColor( 0xff1493, "DeepPink1" ), new NamedColor( 0xee1289, "DeepPink2" ), new NamedColor( 0xcd1076, "DeepPink3" ), new NamedColor( 0x8b0a50, "DeepPink4" ), new NamedColor( 0x00bfff, "DeepSkyBlue" ), new NamedColor( 0x00bfff, "DeepSkyBlue1" ), new NamedColor( 0x00b2ee, "DeepSkyBlue2" ), new NamedColor( 0x009acd, "DeepSkyBlue3" ), new NamedColor( 0x00688b, "DeepSkyBlue4" ), new NamedColor( 0x696969, "DimGray" ), new NamedColor( 0x696969, "DimGrey" ), new NamedColor( 0x1e90ff, "DodgerBlue" ), new NamedColor( 0x1e90ff, "DodgerBlue1" ), new NamedColor( 0x1c86ee, "DodgerBlue2" ), new NamedColor( 0x1874cd, "DodgerBlue3" ), new NamedColor( 0x104e8b, "DodgerBlue4" ), new NamedColor( 0xb22222, "firebrick" ), new NamedColor( 0xff3030, "firebrick1" ), new NamedColor( 0xee2c2c, "firebrick2" ), new NamedColor( 0xcd2626, "firebrick3" ), new NamedColor( 0x8b1a1a, "firebrick4" ), new NamedColor( 0xfffaf0, "FloralWhite" ), new NamedColor( 0x228b22, "ForestGreen" ), new NamedColor( 0xdcdcdc, "gainsboro" ), new NamedColor( 0xf8f8ff, "GhostWhite" ), new NamedColor( 0xffd700, "gold" ), new NamedColor( 0xffd700, "gold1" ), new NamedColor( 0xeec900, "gold2" ), new NamedColor( 0xcdad00, "gold3" ), new NamedColor( 0x8b7500, "gold4" ), new NamedColor( 0xdaa520, "goldenrod" ), new NamedColor( 0xffc125, "goldenrod1" ), new NamedColor( 0xeeb422, "goldenrod2" ), new NamedColor( 0xcd9b1d, "goldenrod3" ), new NamedColor( 0x8b6914, "goldenrod4" ), new NamedColor( 0xbebebe, "gray" ), new NamedColor( 0x000000, "gray0" ), new NamedColor( 0x030303, "gray1" ), new NamedColor( 0x1a1a1a, "gray10" ), new NamedColor( 0xffffff, "gray100" ), new NamedColor( 0x1c1c1c, "gray11" ), new NamedColor( 0x1f1f1f, "gray12" ), new NamedColor( 0x212121, "gray13" ), new NamedColor( 0x242424, "gray14" ), new NamedColor( 0x262626, "gray15" ), new NamedColor( 0x292929, "gray16" ), new NamedColor( 0x2b2b2b, "gray17" ), new NamedColor( 0x2e2e2e, "gray18" ), new NamedColor( 0x303030, "gray19" ), new NamedColor( 0x050505, "gray2" ), new NamedColor( 0x333333, "gray20" ), new NamedColor( 0x363636, "gray21" ), new NamedColor( 0x383838, "gray22" ), new NamedColor( 0x3b3b3b, "gray23" ), new NamedColor( 0x3d3d3d, "gray24" ), new NamedColor( 0x404040, "gray25" ), new NamedColor( 0x424242, "gray26" ), new NamedColor( 0x454545, "gray27" ), new NamedColor( 0x474747, "gray28" ), new NamedColor( 0x4a4a4a, "gray29" ), new NamedColor( 0x080808, "gray3" ), new NamedColor( 0x4d4d4d, "gray30" ), new NamedColor( 0x4f4f4f, "gray31" ), new NamedColor( 0x525252, "gray32" ), new NamedColor( 0x545454, "gray33" ), new NamedColor( 0x575757, "gray34" ), new NamedColor( 0x595959, "gray35" ), new NamedColor( 0x5c5c5c, "gray36" ), new NamedColor( 0x5e5e5e, "gray37" ), new NamedColor( 0x616161, "gray38" ), new NamedColor( 0x636363, "gray39" ), new NamedColor( 0x0a0a0a, "gray4" ), new NamedColor( 0x666666, "gray40" ), new NamedColor( 0x696969, "gray41" ), new NamedColor( 0x6b6b6b, "gray42" ), new NamedColor( 0x6e6e6e, "gray43" ), new NamedColor( 0x707070, "gray44" ), new NamedColor( 0x737373, "gray45" ), new NamedColor( 0x757575, "gray46" ), new NamedColor( 0x787878, "gray47" ), new NamedColor( 0x7a7a7a, "gray48" ), new NamedColor( 0x7d7d7d, "gray49" ), new NamedColor( 0x0d0d0d, "gray5" ), new NamedColor( 0x7f7f7f, "gray50" ), new NamedColor( 0x828282, "gray51" ), new NamedColor( 0x858585, "gray52" ), new NamedColor( 0x878787, "gray53" ), new NamedColor( 0x8a8a8a, "gray54" ), new NamedColor( 0x8c8c8c, "gray55" ), new NamedColor( 0x8f8f8f, "gray56" ), new NamedColor( 0x919191, "gray57" ), new NamedColor( 0x949494, "gray58" ), new NamedColor( 0x969696, "gray59" ), new NamedColor( 0x0f0f0f, "gray6" ), new NamedColor( 0x999999, "gray60" ), new NamedColor( 0x9c9c9c, "gray61" ), new NamedColor( 0x9e9e9e, "gray62" ), new NamedColor( 0xa1a1a1, "gray63" ), new NamedColor( 0xa3a3a3, "gray64" ), new NamedColor( 0xa6a6a6, "gray65" ), new NamedColor( 0xa8a8a8, "gray66" ), new NamedColor( 0xababab, "gray67" ), new NamedColor( 0xadadad, "gray68" ), new NamedColor( 0xb0b0b0, "gray69" ), new NamedColor( 0x121212, "gray7" ), new NamedColor( 0xb3b3b3, "gray70" ), new NamedColor( 0xb5b5b5, "gray71" ), new NamedColor( 0xb8b8b8, "gray72" ), new NamedColor( 0xbababa, "gray73" ), new NamedColor( 0xbdbdbd, "gray74" ), new NamedColor( 0xbfbfbf, "gray75" ), new NamedColor( 0xc2c2c2, "gray76" ), new NamedColor( 0xc4c4c4, "gray77" ), new NamedColor( 0xc7c7c7, "gray78" ), new NamedColor( 0xc9c9c9, "gray79" ), new NamedColor( 0x141414, "gray8" ), new NamedColor( 0xcccccc, "gray80" ), new NamedColor( 0xcfcfcf, "gray81" ), new NamedColor( 0xd1d1d1, "gray82" ), new NamedColor( 0xd4d4d4, "gray83" ), new NamedColor( 0xd6d6d6, "gray84" ), new NamedColor( 0xd9d9d9, "gray85" ), new NamedColor( 0xdbdbdb, "gray86" ), new NamedColor( 0xdedede, "gray87" ), new NamedColor( 0xe0e0e0, "gray88" ), new NamedColor( 0xe3e3e3, "gray89" ), new NamedColor( 0x171717, "gray9" ), new NamedColor( 0xe5e5e5, "gray90" ), new NamedColor( 0xe8e8e8, "gray91" ), new NamedColor( 0xebebeb, "gray92" ), new NamedColor( 0xededed, "gray93" ), new NamedColor( 0xf0f0f0, "gray94" ), new NamedColor( 0xf2f2f2, "gray95" ), new NamedColor( 0xf5f5f5, "gray96" ), new NamedColor( 0xf7f7f7, "gray97" ), new NamedColor( 0xfafafa, "gray98" ), new NamedColor( 0xfcfcfc, "gray99" ), new NamedColor( 0x00ff00, "green" ), new NamedColor( 0x00ff00, "green1" ), new NamedColor( 0x00ee00, "green2" ), new NamedColor( 0x00cd00, "green3" ), new NamedColor( 0x008b00, "green4" ), new NamedColor( 0xadff2f, "GreenYellow" ), new NamedColor( 0xbebebe, "grey" ), new NamedColor( 0x000000, "grey0" ), new NamedColor( 0x030303, "grey1" ), new NamedColor( 0x1a1a1a, "grey10" ), new NamedColor( 0xffffff, "grey100" ), new NamedColor( 0x1c1c1c, "grey11" ), new NamedColor( 0x1f1f1f, "grey12" ), new NamedColor( 0x212121, "grey13" ), new NamedColor( 0x242424, "grey14" ), new NamedColor( 0x262626, "grey15" ), new NamedColor( 0x292929, "grey16" ), new NamedColor( 0x2b2b2b, "grey17" ), new NamedColor( 0x2e2e2e, "grey18" ), new NamedColor( 0x303030, "grey19" ), new NamedColor( 0x050505, "grey2" ), new NamedColor( 0x333333, "grey20" ), new NamedColor( 0x363636, "grey21" ), new NamedColor( 0x383838, "grey22" ), new NamedColor( 0x3b3b3b, "grey23" ), new NamedColor( 0x3d3d3d, "grey24" ), new NamedColor( 0x404040, "grey25" ), new NamedColor( 0x424242, "grey26" ), new NamedColor( 0x454545, "grey27" ), new NamedColor( 0x474747, "grey28" ), new NamedColor( 0x4a4a4a, "grey29" ), new NamedColor( 0x080808, "grey3" ), new NamedColor( 0x4d4d4d, "grey30" ), new NamedColor( 0x4f4f4f, "grey31" ), new NamedColor( 0x525252, "grey32" ), new NamedColor( 0x545454, "grey33" ), new NamedColor( 0x575757, "grey34" ), new NamedColor( 0x595959, "grey35" ), new NamedColor( 0x5c5c5c, "grey36" ), new NamedColor( 0x5e5e5e, "grey37" ), new NamedColor( 0x616161, "grey38" ), new NamedColor( 0x636363, "grey39" ), new NamedColor( 0x0a0a0a, "grey4" ), new NamedColor( 0x666666, "grey40" ), new NamedColor( 0x696969, "grey41" ), new NamedColor( 0x6b6b6b, "grey42" ), new NamedColor( 0x6e6e6e, "grey43" ), new NamedColor( 0x707070, "grey44" ), new NamedColor( 0x737373, "grey45" ), new NamedColor( 0x757575, "grey46" ), new NamedColor( 0x787878, "grey47" ), new NamedColor( 0x7a7a7a, "grey48" ), new NamedColor( 0x7d7d7d, "grey49" ), new NamedColor( 0x0d0d0d, "grey5" ), new NamedColor( 0x7f7f7f, "grey50" ), new NamedColor( 0x828282, "grey51" ), new NamedColor( 0x858585, "grey52" ), new NamedColor( 0x878787, "grey53" ), new NamedColor( 0x8a8a8a, "grey54" ), new NamedColor( 0x8c8c8c, "grey55" ), new NamedColor( 0x8f8f8f, "grey56" ), new NamedColor( 0x919191, "grey57" ), new NamedColor( 0x949494, "grey58" ), new NamedColor( 0x969696, "grey59" ), new NamedColor( 0x0f0f0f, "grey6" ), new NamedColor( 0x999999, "grey60" ), new NamedColor( 0x9c9c9c, "grey61" ), new NamedColor( 0x9e9e9e, "grey62" ), new NamedColor( 0xa1a1a1, "grey63" ), new NamedColor( 0xa3a3a3, "grey64" ), new NamedColor( 0xa6a6a6, "grey65" ), new NamedColor( 0xa8a8a8, "grey66" ), new NamedColor( 0xababab, "grey67" ), new NamedColor( 0xadadad, "grey68" ), new NamedColor( 0xb0b0b0, "grey69" ), new NamedColor( 0x121212, "grey7" ), new NamedColor( 0xb3b3b3, "grey70" ), new NamedColor( 0xb5b5b5, "grey71" ), new NamedColor( 0xb8b8b8, "grey72" ), new NamedColor( 0xbababa, "grey73" ), new NamedColor( 0xbdbdbd, "grey74" ), new NamedColor( 0xbfbfbf, "grey75" ), new NamedColor( 0xc2c2c2, "grey76" ), new NamedColor( 0xc4c4c4, "grey77" ), new NamedColor( 0xc7c7c7, "grey78" ), new NamedColor( 0xc9c9c9, "grey79" ), new NamedColor( 0x141414, "grey8" ), new NamedColor( 0xcccccc, "grey80" ), new NamedColor( 0xcfcfcf, "grey81" ), new NamedColor( 0xd1d1d1, "grey82" ), new NamedColor( 0xd4d4d4, "grey83" ), new NamedColor( 0xd6d6d6, "grey84" ), new NamedColor( 0xd9d9d9, "grey85" ), new NamedColor( 0xdbdbdb, "grey86" ), new NamedColor( 0xdedede, "grey87" ), new NamedColor( 0xe0e0e0, "grey88" ), new NamedColor( 0xe3e3e3, "grey89" ), new NamedColor( 0x171717, "grey9" ), new NamedColor( 0xe5e5e5, "grey90" ), new NamedColor( 0xe8e8e8, "grey91" ), new NamedColor( 0xebebeb, "grey92" ), new NamedColor( 0xededed, "grey93" ), new NamedColor( 0xf0f0f0, "grey94" ), new NamedColor( 0xf2f2f2, "grey95" ), new NamedColor( 0xf5f5f5, "grey96" ), new NamedColor( 0xf7f7f7, "grey97" ), new NamedColor( 0xfafafa, "grey98" ), new NamedColor( 0xfcfcfc, "grey99" ), new NamedColor( 0xf0fff0, "honeydew" ), new NamedColor( 0xf0fff0, "honeydew1" ), new NamedColor( 0xe0eee0, "honeydew2" ), new NamedColor( 0xc1cdc1, "honeydew3" ), new NamedColor( 0x838b83, "honeydew4" ), new NamedColor( 0xff69b4, "HotPink" ), new NamedColor( 0xff6eb4, "HotPink1" ), new NamedColor( 0xee6aa7, "HotPink2" ), new NamedColor( 0xcd6090, "HotPink3" ), new NamedColor( 0x8b3a62, "HotPink4" ), new NamedColor( 0xcd5c5c, "IndianRed" ), new NamedColor( 0xff6a6a, "IndianRed1" ), new NamedColor( 0xee6363, "IndianRed2" ), new NamedColor( 0xcd5555, "IndianRed3" ), new NamedColor( 0x8b3a3a, "IndianRed4" ), new NamedColor( 0xfffff0, "ivory" ), new NamedColor( 0xfffff0, "ivory1" ), new NamedColor( 0xeeeee0, "ivory2" ), new NamedColor( 0xcdcdc1, "ivory3" ), new NamedColor( 0x8b8b83, "ivory4" ), new NamedColor( 0xf0e68c, "khaki" ), new NamedColor( 0xfff68f, "khaki1" ), new NamedColor( 0xeee685, "khaki2" ), new NamedColor( 0xcdc673, "khaki3" ), new NamedColor( 0x8b864e, "khaki4" ), new NamedColor( 0xe6e6fa, "lavender" ), new NamedColor( 0xfff0f5, "LavenderBlush" ), new NamedColor( 0xfff0f5, "LavenderBlush1" ), new NamedColor( 0xeee0e5, "LavenderBlush2" ), new NamedColor( 0xcdc1c5, "LavenderBlush3" ), new NamedColor( 0x8b8386, "LavenderBlush4" ), new NamedColor( 0x7cfc00, "LawnGreen" ), new NamedColor( 0xfffacd, "LemonChiffon" ), new NamedColor( 0xfffacd, "LemonChiffon1" ), new NamedColor( 0xeee9bf, "LemonChiffon2" ), new NamedColor( 0xcdc9a5, "LemonChiffon3" ), new NamedColor( 0x8b8970, "LemonChiffon4" ), new NamedColor( 0xadd8e6, "LightBlue" ), new NamedColor( 0xbfefff, "LightBlue1" ), new NamedColor( 0xb2dfee, "LightBlue2" ), new NamedColor( 0x9ac0cd, "LightBlue3" ), new NamedColor( 0x68838b, "LightBlue4" ), new NamedColor( 0xf08080, "LightCoral" ), new NamedColor( 0xe0ffff, "LightCyan" ), new NamedColor( 0xe0ffff, "LightCyan1" ), new NamedColor( 0xd1eeee, "LightCyan2" ), new NamedColor( 0xb4cdcd, "LightCyan3" ), new NamedColor( 0x7a8b8b, "LightCyan4" ), new NamedColor( 0xfafad2, "LightGoldenrodYellow" ), new NamedColor( 0xeedd82, "LightGoldenrod" ), new NamedColor( 0xffec8b, "LightGoldenrod1" ), new NamedColor( 0xeedc82, "LightGoldenrod2" ), new NamedColor( 0xcdbe70, "LightGoldenrod3" ), new NamedColor( 0x8b814c, "LightGoldenrod4" ), new NamedColor( 0xd3d3d3, "LightGray" ), new NamedColor( 0x90ee90, "LightGreen" ), new NamedColor( 0xd3d3d3, "LightGrey" ), new NamedColor( 0xffb6c1, "LightPink" ), new NamedColor( 0xffaeb9, "LightPink1" ), new NamedColor( 0xeea2ad, "LightPink2" ), new NamedColor( 0xcd8c95, "LightPink3" ), new NamedColor( 0x8b5f65, "LightPink4" ), new NamedColor( 0xffa07a, "LightSalmon" ), new NamedColor( 0xffa07a, "LightSalmon1" ), new NamedColor( 0xee9572, "LightSalmon2" ), new NamedColor( 0xcd8162, "LightSalmon3" ), new NamedColor( 0x8b5742, "LightSalmon4" ), new NamedColor( 0x20b2aa, "LightSeaGreen" ), new NamedColor( 0x87cefa, "LightSkyBlue" ), new NamedColor( 0xb0e2ff, "LightSkyBlue1" ), new NamedColor( 0xa4d3ee, "LightSkyBlue2" ), new NamedColor( 0x8db6cd, "LightSkyBlue3" ), new NamedColor( 0x607b8b, "LightSkyBlue4" ), new NamedColor( 0x8470ff, "LightSlateBlue" ), new NamedColor( 0x778899, "LightSlateGray" ), new NamedColor( 0x778899, "LightSlateGrey" ), new NamedColor( 0xb0c4de, "LightSteelBlue" ), new NamedColor( 0xcae1ff, "LightSteelBlue1" ), new NamedColor( 0xbcd2ee, "LightSteelBlue2" ), new NamedColor( 0xa2b5cd, "LightSteelBlue3" ), new NamedColor( 0x6e7b8b, "LightSteelBlue4" ), new NamedColor( 0xffffe0, "LightYellow" ), new NamedColor( 0xffffe0, "LightYellow1" ), new NamedColor( 0xeeeed1, "LightYellow2" ), new NamedColor( 0xcdcdb4, "LightYellow3" ), new NamedColor( 0x8b8b7a, "LightYellow4" ), new NamedColor( 0x32cd32, "LimeGreen" ), new NamedColor( 0xfaf0e6, "linen" ), new NamedColor( 0xff00ff, "magenta" ), new NamedColor( 0xff00ff, "magenta1" ), new NamedColor( 0xee00ee, "magenta2" ), new NamedColor( 0xcd00cd, "magenta3" ), new NamedColor( 0x8b008b, "magenta4" ), new NamedColor( 0xb03060, "maroon" ), new NamedColor( 0xff34b3, "maroon1" ), new NamedColor( 0xee30a7, "maroon2" ), new NamedColor( 0xcd2990, "maroon3" ), new NamedColor( 0x8b1c62, "maroon4" ), new NamedColor( 0x66cdaa, "MediumAquamarine" ), new NamedColor( 0x0000cd, "MediumBlue" ), new NamedColor( 0xba55d3, "MediumOrchid" ), new NamedColor( 0xe066ff, "MediumOrchid1" ), new NamedColor( 0xd15fee, "MediumOrchid2" ), new NamedColor( 0xb452cd, "MediumOrchid3" ), new NamedColor( 0x7a378b, "MediumOrchid4" ), new NamedColor( 0x9370db, "MediumPurple" ), new NamedColor( 0xab82ff, "MediumPurple1" ), new NamedColor( 0x9f79ee, "MediumPurple2" ), new NamedColor( 0x8968cd, "MediumPurple3" ), new NamedColor( 0x5d478b, "MediumPurple4" ), new NamedColor( 0x3cb371, "MediumSeaGreen" ), new NamedColor( 0x7b68ee, "MediumSlateBlue" ), new NamedColor( 0x00fa9a, "MediumSpringGreen" ), new NamedColor( 0x48d1cc, "MediumTurquoise" ), new NamedColor( 0xc71585, "MediumVioletRed" ), new NamedColor( 0x191970, "MidnightBlue" ), new NamedColor( 0xf5fffa, "MintCream" ), new NamedColor( 0xffe4e1, "MistyRose" ), new NamedColor( 0xffe4e1, "MistyRose1" ), new NamedColor( 0xeed5d2, "MistyRose2" ), new NamedColor( 0xcdb7b5, "MistyRose3" ), new NamedColor( 0x8b7d7b, "MistyRose4" ), new NamedColor( 0xffe4b5, "moccasin" ), new NamedColor( 0xffdead, "NavajoWhite" ), new NamedColor( 0xffdead, "NavajoWhite1" ), new NamedColor( 0xeecfa1, "NavajoWhite2" ), new NamedColor( 0xcdb38b, "NavajoWhite3" ), new NamedColor( 0x8b795e, "NavajoWhite4" ), new NamedColor( 0x000080, "navy" ), new NamedColor( 0x000080, "NavyBlue" ), new NamedColor( 0xfdf5e6, "OldLace" ), new NamedColor( 0x6b8e23, "OliveDrab" ), new NamedColor( 0xc0ff3e, "OliveDrab1" ), new NamedColor( 0xb3ee3a, "OliveDrab2" ), new NamedColor( 0x9acd32, "OliveDrab3" ), new NamedColor( 0x698b22, "OliveDrab4" ), new NamedColor( 0xffa500, "orange" ), new NamedColor( 0xffa500, "orange1" ), new NamedColor( 0xee9a00, "orange2" ), new NamedColor( 0xcd8500, "orange3" ), new NamedColor( 0x8b5a00, "orange4" ), new NamedColor( 0xff4500, "OrangeRed" ), new NamedColor( 0xff4500, "OrangeRed1" ), new NamedColor( 0xee4000, "OrangeRed2" ), new NamedColor( 0xcd3700, "OrangeRed3" ), new NamedColor( 0x8b2500, "OrangeRed4" ), new NamedColor( 0xda70d6, "orchid" ), new NamedColor( 0xff83fa, "orchid1" ), new NamedColor( 0xee7ae9, "orchid2" ), new NamedColor( 0xcd69c9, "orchid3" ), new NamedColor( 0x8b4789, "orchid4" ), new NamedColor( 0xeee8aa, "PaleGoldenrod" ), new NamedColor( 0x98fb98, "PaleGreen" ), new NamedColor( 0x9aff9a, "PaleGreen1" ), new NamedColor( 0x90ee90, "PaleGreen2" ), new NamedColor( 0x7ccd7c, "PaleGreen3" ), new NamedColor( 0x548b54, "PaleGreen4" ), new NamedColor( 0xafeeee, "PaleTurquoise" ), new NamedColor( 0xbbffff, "PaleTurquoise1" ), new NamedColor( 0xaeeeee, "PaleTurquoise2" ), new NamedColor( 0x96cdcd, "PaleTurquoise3" ), new NamedColor( 0x668b8b, "PaleTurquoise4" ), new NamedColor( 0xdb7093, "PaleVioletRed" ), new NamedColor( 0xff82ab, "PaleVioletRed1" ), new NamedColor( 0xee799f, "PaleVioletRed2" ), new NamedColor( 0xcd6889, "PaleVioletRed3" ), new NamedColor( 0x8b475d, "PaleVioletRed4" ), new NamedColor( 0xffefd5, "PapayaWhip" ), new NamedColor( 0xffdab9, "PeachPuff" ), new NamedColor( 0xffdab9, "PeachPuff1" ), new NamedColor( 0xeecbad, "PeachPuff2" ), new NamedColor( 0xcdaf95, "PeachPuff3" ), new NamedColor( 0x8b7765, "PeachPuff4" ), new NamedColor( 0xcd853f, "peru" ), new NamedColor( 0xffc0cb, "pink" ), new NamedColor( 0xffb5c5, "pink1" ), new NamedColor( 0xeea9b8, "pink2" ), new NamedColor( 0xcd919e, "pink3" ), new NamedColor( 0x8b636c, "pink4" ), new NamedColor( 0xdda0dd, "plum" ), new NamedColor( 0xffbbff, "plum1" ), new NamedColor( 0xeeaeee, "plum2" ), new NamedColor( 0xcd96cd, "plum3" ), new NamedColor( 0x8b668b, "plum4" ), new NamedColor( 0xb0e0e6, "PowderBlue" ), new NamedColor( 0xa020f0, "purple" ), new NamedColor( 0x9b30ff, "purple1" ), new NamedColor( 0x912cee, "purple2" ), new NamedColor( 0x7d26cd, "purple3" ), new NamedColor( 0x551a8b, "purple4" ), new NamedColor( 0xff0000, "red" ), new NamedColor( 0xff0000, "red1" ), new NamedColor( 0xee0000, "red2" ), new NamedColor( 0xcd0000, "red3" ), new NamedColor( 0x8b0000, "red4" ), new NamedColor( 0xbc8f8f, "RosyBrown" ), new NamedColor( 0xffc1c1, "RosyBrown1" ), new NamedColor( 0xeeb4b4, "RosyBrown2" ), new NamedColor( 0xcd9b9b, "RosyBrown3" ), new NamedColor( 0x8b6969, "RosyBrown4" ), new NamedColor( 0x4169e1, "RoyalBlue" ), new NamedColor( 0x4876ff, "RoyalBlue1" ), new NamedColor( 0x436eee, "RoyalBlue2" ), new NamedColor( 0x3a5fcd, "RoyalBlue3" ), new NamedColor( 0x27408b, "RoyalBlue4" ), new NamedColor( 0x8b4513, "SaddleBrown" ), new NamedColor( 0xfa8072, "salmon" ), new NamedColor( 0xff8c69, "salmon1" ), new NamedColor( 0xee8262, "salmon2" ), new NamedColor( 0xcd7054, "salmon3" ), new NamedColor( 0x8b4c39, "salmon4" ), new NamedColor( 0xf4a460, "SandyBrown" ), new NamedColor( 0x2e8b57, "SeaGreen" ), new NamedColor( 0x54ff9f, "SeaGreen1" ), new NamedColor( 0x4eee94, "SeaGreen2" ), new NamedColor( 0x43cd80, "SeaGreen3" ), new NamedColor( 0x2e8b57, "SeaGreen4" ), new NamedColor( 0xfff5ee, "seashell" ), new NamedColor( 0xfff5ee, "seashell1" ), new NamedColor( 0xeee5de, "seashell2" ), new NamedColor( 0xcdc5bf, "seashell3" ), new NamedColor( 0x8b8682, "seashell4" ), new NamedColor( 0xa0522d, "sienna" ), new NamedColor( 0xff8247, "sienna1" ), new NamedColor( 0xee7942, "sienna2" ), new NamedColor( 0xcd6839, "sienna3" ), new NamedColor( 0x8b4726, "sienna4" ), new NamedColor( 0x87ceeb, "SkyBlue" ), new NamedColor( 0x87ceff, "SkyBlue1" ), new NamedColor( 0x7ec0ee, "SkyBlue2" ), new NamedColor( 0x6ca6cd, "SkyBlue3" ), new NamedColor( 0x4a708b, "SkyBlue4" ), new NamedColor( 0x6a5acd, "SlateBlue" ), new NamedColor( 0x836fff, "SlateBlue1" ), new NamedColor( 0x7a67ee, "SlateBlue2" ), new NamedColor( 0x6959cd, "SlateBlue3" ), new NamedColor( 0x473c8b, "SlateBlue4" ), new NamedColor( 0x708090, "SlateGray" ), new NamedColor( 0xc6e2ff, "SlateGray1" ), new NamedColor( 0xb9d3ee, "SlateGray2" ), new NamedColor( 0x9fb6cd, "SlateGray3" ), new NamedColor( 0x6c7b8b, "SlateGray4" ), new NamedColor( 0x708090, "SlateGrey" ), new NamedColor( 0xfffafa, "snow" ), new NamedColor( 0xfffafa, "snow1" ), new NamedColor( 0xeee9e9, "snow2" ), new NamedColor( 0xcdc9c9, "snow3" ), new NamedColor( 0x8b8989, "snow4" ), new NamedColor( 0x00ff7f, "SpringGreen" ), new NamedColor( 0x00ff7f, "SpringGreen1" ), new NamedColor( 0x00ee76, "SpringGreen2" ), new NamedColor( 0x00cd66, "SpringGreen3" ), new NamedColor( 0x008b45, "SpringGreen4" ), new NamedColor( 0x4682b4, "SteelBlue" ), new NamedColor( 0x63b8ff, "SteelBlue1" ), new NamedColor( 0x5cacee, "SteelBlue2" ), new NamedColor( 0x4f94cd, "SteelBlue3" ), new NamedColor( 0x36648b, "SteelBlue4" ), new NamedColor( 0xd2b48c, "tan" ), new NamedColor( 0xffa54f, "tan1" ), new NamedColor( 0xee9a49, "tan2" ), new NamedColor( 0xcd853f, "tan3" ), new NamedColor( 0x8b5a2b, "tan4" ), new NamedColor( 0xd8bfd8, "thistle" ), new NamedColor( 0xffe1ff, "thistle1" ), new NamedColor( 0xeed2ee, "thistle2" ), new NamedColor( 0xcdb5cd, "thistle3" ), new NamedColor( 0x8b7b8b, "thistle4" ), new NamedColor( 0xff6347, "tomato" ), new NamedColor( 0xff6347, "tomato1" ), new NamedColor( 0xee5c42, "tomato2" ), new NamedColor( 0xcd4f39, "tomato3" ), new NamedColor( 0x8b3626, "tomato4" ), new NamedColor( 0x40e0d0, "turquoise" ), new NamedColor( 0x00f5ff, "turquoise1" ), new NamedColor( 0x00e5ee, "turquoise2" ), new NamedColor( 0x00c5cd, "turquoise3" ), new NamedColor( 0x00868b, "turquoise4" ), new NamedColor( 0xee82ee, "violet" ), new NamedColor( 0xd02090, "VioletRed" ), new NamedColor( 0xff3e96, "VioletRed1" ), new NamedColor( 0xee3a8c, "VioletRed2" ), new NamedColor( 0xcd3278, "VioletRed3" ), new NamedColor( 0x8b2252, "VioletRed4" ), new NamedColor( 0xf5deb3, "wheat" ), new NamedColor( 0xffe7ba, "wheat1" ), new NamedColor( 0xeed8ae, "wheat2" ), new NamedColor( 0xcdba96, "wheat3" ), new NamedColor( 0x8b7e66, "wheat4" ), new NamedColor( 0xffffff, "white" ), new NamedColor( 0xf5f5f5, "WhiteSmoke" ), new NamedColor( 0xffff00, "yellow" ), new NamedColor( 0xffff00, "yellow1" ), new NamedColor( 0xeeee00, "yellow2" ), new NamedColor( 0xcdcd00, "yellow3" ), new NamedColor( 0x8b8b00, "yellow4" ), new NamedColor( 0x9acd32, "YellowGreen" ), }; private Color[] currentColor; private JComboBox /**/[] selectedColorName; private JLabel[][] hsbLab; private JLabel[][] rgbLab; private JPanel[] colorPanel; private JPanel[] topPanel; private JSlider[][] hsbSlider; private JSlider[][] rgbSlider; private JSpinner[][] hsbDecSpinner; private JSpinner[][] hsbHexSpinner; private JSpinner[][] rgbDecSpinner; private JSpinner[][] rgbHexSpinner; private JSpinner[] selectedColorHexSpinner; private JTextArea sample; /** * true when a propagate in executing, stops infinite regress */ private boolean propInProgress; /** * get the value of a JSlider * * @param j the JSlider * * @return current value of the slider */ private static int getValue( JSlider j ) { return j.getModel().getValue(); // int } /** * get the value of a JSpinner * * @param j the JSpinner * * @return current value of the slider */ private static int getValue( JSpinner j ) { return ( ( SpinnerNumberModel ) j.getModel() ).getNumber().intValue(); } /** * Set the value of a JSpinner * * @param j the JSpinner * @param value new value for the spinner */ private static void setValue( JSpinner j, int value ) { j.getModel().setValue( value ); } /** * Set the value of a JSpinner * * @param j the JSpinner * @param color new color for the spinner */ private static void setValue( JSpinner j, Color color ) { j.getModel().setValue( color.getRGB() & 0xffffff ); } /** * Set the value of a JSlider * * @param j the JSlider * @param value new value for the slider */ private static void setValue( JSlider j, int value ) { j.getModel().setValue( value ); } /** * allocate and initialise widgets * * @param fore 0=background 1=foreground */ private void allocateAndInitialiseWidgets( int fore ) { currentColor[ fore ] = ( fore == 1 ) ? FOREGROUND_FOR_INITIAL : BACKGROUND_FOR_INITIAL; selectedColorName[ fore ] = new JComboBox<>( x11Palette ); for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { rgbLab[ fore ][ rgbIndex ] = new JLabel( rgbLabels[ rgbIndex ] ); rgbLab[ fore ][ rgbIndex ].setForeground( FOREGROUND_FOR_LABEL ); rgbLab[ fore ][ rgbIndex ].setBackground( BACKGROUND_FOR_LABEL ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { hsbLab[ fore ][ hsbIndex ] = new JLabel( hsbLabels[ hsbIndex ] ); hsbLab[ fore ][ hsbIndex ].setForeground( FOREGROUND_FOR_LABEL ); rgbLab[ fore ][ hsbIndex ].setBackground( BACKGROUND_FOR_LABEL ); } colorPanel[ fore ] = new JPanel(); colorPanel[ fore ].setBorder( new TitledBorder( BorderFactory.createLineBorder( SLATEBLUE ), " " + backForeLabels[ fore ] + " ", TitledBorder.LEADING, TitledBorder.TOP, FontFactory.build( "Dialog", Font.ITALIC, 16 ), FOREGROUND_FOR_LABEL ) ); topPanel[ fore ] = new JPanel(); for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { rgbSlider[ fore ][ rgbIndex ] = new JSlider( JSlider.VERTICAL, 0, 255, 127 ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { hsbSlider[ fore ][ hsbIndex ] = new JSlider( JSlider.VERTICAL, 0, 100, 50 ); } sample = new JTextArea( TEXT_SAMPLE ); sample.setEditable( false ); sample.setForeground( FOREGROUND_FOR_INITIAL ); sample.setBackground( BACKGROUND_FOR_INITIAL ); sample.setFont( FontFactory.build( "Dialog", Font.PLAIN, 12 ) ); for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { rgbDecSpinner[ fore ][ rgbIndex ] = new JSpinner( new SpinnerNumberModel( 127, 0, 255, 1 ) ); // setBackground on JSpinner has no effect // apply pale red, green, blue. final Color background; switch ( rgbIndex ) { case 0: background = PALERED; break; case 1: background = PALEGREEN; break; case 2: background = PALEBLUE; break; default: throw new IllegalArgumentException( "bug in switch" ); } JFormattedTextField innerSpinner = ( ( JSpinner.DefaultEditor ) rgbDecSpinner[ fore ][ rgbIndex ] .getEditor() ) .getTextField(); innerSpinner .setBackground( background ); innerSpinner.setMargin( new Insets( 1, 1, 1, 1 ) ); } for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { rgbHexSpinner[ fore ][ rgbIndex ] = new JSpinner( new SpinnerNumberModel( 127, 0, 255, 1 ) ); HexNumberEditor hne = new HexNumberEditor( rgbHexSpinner[ fore ][ rgbIndex ], 2 ); rgbHexSpinner[ fore ][ rgbIndex ].setEditor( hne ); // setBackground on JSpinner has no effect. JFormattedTextField innerSpinner = hne.getTextField(); innerSpinner.setForeground( FOREGROUND_FOR_HEX ); innerSpinner.setBackground( BACKGROUND_FOR_HEX ); innerSpinner.setMargin( new Insets( 1, 1, 1, 1 ) ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { hsbDecSpinner[ fore ][ hsbIndex ] = new JSpinner( new SpinnerNumberModel( 50, 0, 100, 1 ) ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { hsbHexSpinner[ fore ][ hsbIndex ] = new JSpinner( new SpinnerNumberModel( 50, 0, 100, 1 ) ); HexNumberEditor hne = new HexNumberEditor( hsbHexSpinner[ fore ][ hsbIndex ], 2 ); hsbHexSpinner[ fore ][ hsbIndex ].setEditor( hne ); // setBackground on JSpinner has no effect. JFormattedTextField innerSpinner = hne.getTextField(); innerSpinner.setForeground( FOREGROUND_FOR_HEX ); innerSpinner.setBackground( BACKGROUND_FOR_HEX ); innerSpinner.setMargin( new Insets( 1, 1, 1, 1 ) ); } selectedColorHexSpinner[ fore ] = new JSpinner( new SpinnerNumberModel( 128, 0, 0xffffff, 1 ) ); HexNumberEditor hne = new HexNumberEditor( selectedColorHexSpinner[ fore ], 6 ); // setBackground on JSpinner has no effect. hne.getTextField().setBackground( BACKGROUND_FOR_HEX ); hne.getTextField().setForeground( FOREGROUND_FOR_HEX ); selectedColorHexSpinner[ fore ].setEditor( hne ); propCurrentToSample( fore ); propCurrentToRGB( fore ); propCurrentToHSB( fore ); } /** * allocate arrays of Widgets */ private void allocateWidgetArrays() { // allocate arrays currentColor = new Color[ 2 ]; selectedColorName = new JComboBox /* */[ 2 ]; hsbLab = new JLabel[ 2 ][ 3 ]; rgbLab = new JLabel[ 2 ][ 3 ]; colorPanel = new JPanel[ 2 ]; topPanel = new JPanel[ 2 ]; hsbSlider = new JSlider[ 2 ][ 3 ]; rgbSlider = new JSlider[ 2 ][ 3 ]; hsbDecSpinner = new JSpinner[ 2 ][ 3 ]; hsbHexSpinner = new JSpinner[ 2 ][ 3 ]; rgbDecSpinner = new JSpinner[ 2 ][ 3 ]; rgbHexSpinner = new JSpinner[ 2 ][ 3 ]; selectedColorHexSpinner = new JSpinner[ 2 ]; } /** * find named colour closest to one on the sliders * * @param fore 0=background 1=foreground */ private void findClosestNamedColor( int fore ) { int howClose = Integer.MAX_VALUE; NamedColor best = null; final Color c = currentColor[ fore ]; for ( NamedColor n : x11Palette ) { final int diff = Math.abs( n.getRed() - c.getRed() ) + Math.abs( n.getGreen() - c.getGreen() ) + Math.abs( n.getBlue() - c.getBlue() ); if ( diff < howClose ) { best = n; howClose = diff; } } // end for // set name to this colour, but not sliders. // call avoids triggering an event chain created by setting. selectedColorName[ fore ].setSelectedItem( best ); } /** * hook up Listeners */ private void hook() { for ( int fore = 0; fore < 2; fore++ ) { // must be final to be accessible to Listeners. final int foref = fore; selectedColorName[ foref ].addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent e ) { NamedColor namedColor = ( NamedColor ) selectedColorName[ foref ].getSelectedItem(); currentColor[ foref ] = namedColor.getColor(); propCurrentToSample( foref ); propCurrentToRGB( foref ); propCurrentToHSB( foref ); } } ); selectedColorHexSpinner[ foref ].addChangeListener( new ChangeListener() { /** * Invoked when the target of the listener has changed its * state. * @param e a ChangeEvent object */ public void stateChanged( ChangeEvent e ) { currentColor[ foref ] = new Color( getValue( selectedColorHexSpinner[ foref ] ) ); propCurrentToSample( foref ); propCurrentToRGB( foref ); propCurrentToHSB( foref ); } } ); for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { final JSlider slider = rgbSlider[ fore ][ rgbIndex ]; final JSpinner decSpinner = rgbDecSpinner[ fore ][ rgbIndex ]; final JSpinner hexSpinner = rgbHexSpinner[ fore ][ rgbIndex ]; slider.addChangeListener( new ChangeListener() { /** * Invoked when the target of the listener has changed its * state. * @param e a ChangeEvent object */ public void stateChanged( ChangeEvent e ) { final int value = getValue( slider ); setValue( decSpinner, value ); setValue( hexSpinner, value ); propRGBToCurrent( foref ); propCurrentToSample( foref ); propCurrentToHSB( foref ); } } ); decSpinner.addChangeListener( new ChangeListener() { /** * Invoked when the target of the listener has changed its * state. * @param e a ChangeEvent object */ public void stateChanged( ChangeEvent e ) { final int value = getValue( decSpinner ); setValue( slider, value ); setValue( hexSpinner, value ); propRGBToCurrent( foref ); propCurrentToSample( foref ); propCurrentToHSB( foref ); } } ); hexSpinner.addChangeListener( new ChangeListener() { /** * Invoked when the target of the listener has changed its * state. * @param e a ChangeEvent object */ public void stateChanged( ChangeEvent e ) { final int value = getValue( hexSpinner ); setValue( slider, value ); setValue( decSpinner, value ); propRGBToCurrent( foref ); propCurrentToSample( foref ); propCurrentToHSB( foref ); } } ); } // end for for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { final JSlider slider = hsbSlider[ fore ][ hsbIndex ]; final JSpinner decSpinner = hsbDecSpinner[ fore ][ hsbIndex ]; final JSpinner hexSpinner = hsbHexSpinner[ fore ][ hsbIndex ]; slider.addChangeListener( new ChangeListener() { /** * Invoked when the target of the listener has changed its * state. * @param e a ChangeEvent object */ public void stateChanged( ChangeEvent e ) { final int value = getValue( slider ); setValue( decSpinner, value ); setValue( hexSpinner, value ); propHSBToCurrent( foref ); propCurrentToSample( foref ); propCurrentToRGB( foref ); } } ); decSpinner.addChangeListener( new ChangeListener() { /** * Invoked when the target of the listener has changed its * state. * @param e a ChangeEvent object */ public void stateChanged( ChangeEvent e ) { final int value = getValue( decSpinner ); setValue( slider, value ); setValue( hexSpinner, value ); propHSBToCurrent( foref ); propCurrentToSample( foref ); propCurrentToRGB( foref ); } } ); hexSpinner.addChangeListener( new ChangeListener() { /** * Invoked when the target of the listener has changed its * state. * @param e a ChangeEvent object */ public void stateChanged( ChangeEvent e ) { final int value = getValue( hexSpinner ); setValue( slider, value ); setValue( decSpinner, value ); propHSBToCurrent( foref ); propCurrentToSample( foref ); propCurrentToRGB( foref ); } } ); } // end for } // end for } /** * layout wigets for either background or foreground fields. * * @param fore 0=background 1=foreground */ private void layoutColorPanel( int fore ) { // back panel // 0---1-----2----3---4----5- // spare--------------------- 0 // [color name--- xxx --hex---] 1 // red green blue Hue Sat Bri 2 // dd dd dd dd dd dd 3 // | | | | | | 4 // hh hh hh hh hh hh 5 // 0----1----2----3---4---5- colorPanel[ fore ].setLayout( new GridBagLayout() ); colorPanel[ fore ].add( topPanel[ fore ], new GridBagConstraints( 0, 1, 6, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( OUTER_BORDER, OUTER_BORDER, OUTER_BORDER, OUTER_BORDER ), 0, 0 ) ); for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { colorPanel[ fore ].add( rgbLab[ fore ][ rgbIndex ], new GridBagConstraints( rgbIndex, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, ( rgbIndex == 0 ) ? OUTER_BORDER : INNER_BORDER, INNER_BORDER, INNER_BORDER ), 0, 0 ) ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { colorPanel[ fore ].add( hsbLab[ fore ][ hsbIndex ], new GridBagConstraints( hsbIndex + 3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, INNER_BORDER, INNER_BORDER, hsbIndex == 2 ? OUTER_BORDER : INNER_BORDER ), 0, 0 ) ); } for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { colorPanel[ fore ].add( rgbDecSpinner[ fore ][ rgbIndex ], new GridBagConstraints( rgbIndex, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, rgbIndex == 0 ? OUTER_BORDER : INNER_BORDER, INNER_BORDER, INNER_BORDER ), SPINNER_PAD, 0 ) ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { colorPanel[ fore ].add( hsbDecSpinner[ fore ][ hsbIndex ], new GridBagConstraints( hsbIndex + 3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, INNER_BORDER, INNER_BORDER, hsbIndex == 2 ? OUTER_BORDER : INNER_BORDER ), SPINNER_PAD, 0 ) ); } for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { colorPanel[ fore ].add( rgbSlider[ fore ][ rgbIndex ], new GridBagConstraints( rgbIndex, 4, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, rgbIndex == 0 ? OUTER_BORDER : INNER_BORDER, INNER_BORDER, INNER_BORDER ), 0, SLIDER_PAD ) ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { colorPanel[ fore ].add( hsbSlider[ fore ][ hsbIndex ], new GridBagConstraints( hsbIndex + 3, 4, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, INNER_BORDER, INNER_BORDER, hsbIndex == 2 ? OUTER_BORDER : INNER_BORDER ), 0, SLIDER_PAD ) ); } // end for for ( int rgbIndex = 0; rgbIndex < 3; rgbIndex++ ) { colorPanel[ fore ].add( rgbHexSpinner[ fore ][ rgbIndex ], new GridBagConstraints( rgbIndex, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, rgbIndex == 0 ? OUTER_BORDER : INNER_BORDER, OUTER_BORDER, INNER_BORDER ), SPINNER_PAD, 0 ) ); } for ( int hsbIndex = 0; hsbIndex < 3; hsbIndex++ ) { colorPanel[ fore ].add( hsbHexSpinner[ fore ][ hsbIndex ], new GridBagConstraints( hsbIndex + 3, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( INNER_BORDER, INNER_BORDER, OUTER_BORDER, hsbIndex == 2 ? OUTER_BORDER : INNER_BORDER ), SPINNER_PAD, 0 ) ); } } // end for /** * Layout all fields as 5 subpanels */ private void layoutFields() { setLayout( new GridBagLayout() ); for ( int fore = 0; fore < 2; fore++ ) { layoutTopPanel( fore ); layoutColorPanel( fore ); } add( sample, new GridBagConstraints( 0, 0, 6, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( OUTER_BORDER, OUTER_BORDER, INNER_BORDER, OUTER_BORDER ), 0, 0 ) ); add( colorPanel[ 0 ], new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( INNER_BORDER, OUTER_BORDER, OUTER_BORDER, INNER_BORDER ), 0, 0 ) ); add( colorPanel[ 1 ], new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( INNER_BORDER, INNER_BORDER, OUTER_BORDER, OUTER_BORDER ), 0, 0 ) ); } /** * Layout tob panel for either background or foreground containing name and hex value of selected colour. * * @param fore 0=background 1=foreground */ private void layoutTopPanel( int fore ) { topPanel[ fore ].setLayout( new GridBagLayout() ); topPanel[ fore ].add( selectedColorName[ fore ], new GridBagConstraints( 0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); topPanel[ fore ].add( new JLabel() /* dummy spacer */, new GridBagConstraints( 3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 30, 0 ) ); topPanel[ fore ].add( selectedColorHexSpinner[ fore ], new GridBagConstraints( 4, 0, 2, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); } /** * propagate a change in Current to HSB widgets * * @param fore 0=background 1=foreground */ private void propCurrentToHSB( int fore ) { // avoid changes triggering infinite propagation. if ( propInProgress ) { return; } propInProgress = true; int red = currentColor[ fore ].getRed(); int green = currentColor[ fore ].getGreen(); int blue = currentColor[ fore ].getBlue(); float[] results = new float[ 3 ]; Color.RGBtoHSB( red, green, blue, results ); int hue = Math.min( Math.max( 0, ( int ) ( results[ 0 ] * 100f + .5f ) ), 100 ); int sat = Math.min( Math.max( 0, ( int ) ( results[ 1 ] * 100f + .5f ) ), 100 ); int bri = Math.min( Math.max( 0, ( int ) ( results[ 2 ] * 100f + .5f ) ), 100 ); setValue( hsbDecSpinner[ fore ][ 0 ], hue ); setValue( hsbDecSpinner[ fore ][ 1 ], sat ); setValue( hsbDecSpinner[ fore ][ 2 ], bri ); setValue( hsbSlider[ fore ][ 0 ], hue ); setValue( hsbSlider[ fore ][ 1 ], sat ); setValue( hsbSlider[ fore ][ 2 ], bri ); setValue( hsbHexSpinner[ fore ][ 0 ], hue ); setValue( hsbHexSpinner[ fore ][ 1 ], sat ); setValue( hsbHexSpinner[ fore ][ 2 ], bri ); propInProgress = false; } /** * propagate a change in Current color to RGB widgets * * @param fore 0=background 1=foreground */ private void propCurrentToRGB( int fore ) { // avoid changes triggering infinite propagation. if ( propInProgress ) { return; } propInProgress = true; // propagate color to all sliders and spinners int red = currentColor[ fore ].getRed(); int green = currentColor[ fore ].getGreen(); int blue = currentColor[ fore ].getBlue(); setValue( rgbDecSpinner[ fore ][ 0 ], red ); setValue( rgbDecSpinner[ fore ][ 1 ], green ); setValue( rgbDecSpinner[ fore ][ 2 ], blue ); setValue( rgbSlider[ fore ][ 0 ], red ); setValue( rgbSlider[ fore ][ 1 ], green ); setValue( rgbSlider[ fore ][ 2 ], blue ); setValue( rgbHexSpinner[ fore ][ 0 ], red ); setValue( rgbHexSpinner[ fore ][ 1 ], green ); setValue( rgbHexSpinner[ fore ][ 2 ], blue ); propInProgress = false; } /** * propagate the current colour to the sample and to the hex display. * * @param fore 0=background 1=foreground */ private void propCurrentToSample( int fore ) { // avoid changes triggering infinite propagation. if ( propInProgress ) { return; } propInProgress = true; if ( fore == 1 ) { sample.setForeground( currentColor[ fore ] ); } else { sample.setBackground( currentColor[ fore ] ); } // not really a named colour, we just want the hex setValue( selectedColorHexSpinner[ fore ], currentColor[ fore ] ); findClosestNamedColor( fore ); propInProgress = false; } /** * propagate a change in HSB to Current color * * @param fore 0=background 1=foreground */ private void propHSBToCurrent( int fore ) { // propagate color to all sliders and spinners int hue = getValue( hsbDecSpinner[ fore ][ 0 ] ); int sat = getValue( hsbDecSpinner[ fore ][ 1 ] ); int bri = getValue( hsbDecSpinner[ fore ][ 2 ] ); currentColor[ fore ] = Color.getHSBColor( hue / 100f, sat / 100f, bri / 100f ); } /** * propagate a change in RGB to Current colors * * @param fore 0=background 1=foreground */ private void propRGBToCurrent( int fore ) { int red = getValue( rgbDecSpinner[ fore ][ 0 ] ); int green = getValue( rgbDecSpinner[ fore ][ 1 ] ); int blue = getValue( rgbDecSpinner[ fore ][ 2 ] ); currentColor[ fore ] = new Color( red, green, blue ); } /** * Allow this Applet to run as an application as well. * * @param args command line arguments ignored. */ public static void main( String args[] ) { if ( DEBUGGING ) { Mockup.main( args ); } } // end main public void addNotify() { super.addNotify(); allocateWidgetArrays(); // do for both foreground and background for ( int fore = 0; fore < 2; fore++ ) { // allocate widgets allocateAndInitialiseWidgets( fore ); } layoutFields(); hook(); revalidate(); } }