Java Swing issue - Using color palette

北城余情 提交于 2019-11-27 15:51:17

I achieved this by Java Reflection : (works for static final Color defined in java.awt.Color)

Here is my code :

public static String getNameReflection(Color colorParam) {
        try {
            //first read all fields in array
            Field[] field = Class.forName("java.awt.Color").getDeclaredFields();
            for (Field f : field) {
                String colorName = f.getName();
                Class<?> t = f.getType();
                // System.out.println(f.getType());
                // check only for constants - "public static final Color"
                if (t == java.awt.Color.class) {
                    Color defined = (Color) f.get(null);
                    if (defined.equals(colorParam)) {
                        System.out.println(colorName);
                        return colorName.toUpperCase();
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("Error... " + e.toString());
        }
        return "NO_MATCH";
    }

Source : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-reflection-getting-name-of-color.html

RGB is not a very best color model to work with in this situation. HSB would be more appropriate.

  1. Convert RGB to HSB:

    float[] hsb = Color.RGBtoHSB(red, green, blue, null);
    
  2. Detect color:

    String color;
    
    if (hsb[0] >= 0.0 && ksb[0] <= 0.1) {
        color = "Red";
    } else if (hsb[0] > 0.1 && ksb[0] <= 0.25) {
        color = "Orange";
    } else if (hsb[0] > 0.25 && ksb[0] <= 0.35) {
        color = "Yellow";
    } ...
    
trashgod

For a fixed palette, an enum is a reasonable choice, shown in context here:

private enum Hue {

    Cyan(Color.cyan), Magenta(Color.magenta), Yellow(Color.yellow),
    Red(Color.red), Green(Color.green), Blue(Color.blue);

    private final Color color;

    private Hue(Color color) {
        this.color = color;
    }

    public Color getColor() {
        return color;
    }
}

For a variable palette, you need to define a data structure that relates color and name, such as Map<Color, String>. You may also want to look at How to Use Color Choosers: Creating a Custom Chooser Panel. Finally, you may want to consider using existing, standard color names.

I found this thread via mKorbel's link to my Interactive Color Wheel. The applet includes a Java port and extension of Chirag Mehta's Name That Color Javascript library. Since I eventually added the ability to have multiple color name dictionaries, I removed the actual hex/name pairs from the source code and added them as properties files. You only need the first two files if all you want is Chirag's color name dictionary (a mishmash of several smaller dictionaries).

NTC.java is written with a main() method so that it can be tested standalone in a command shell:

>java us.r0k.ntc.NTC 28f369
>  #0BDA51, Malachite, false

The first value is the closest hex to the desired hex, second is the color name for that value, and third indicates that no exact match was found.

You can also specify a second parameter, the name of the color name dictionary (which defaults to "cnd_ntc.properties").

public void updateChooser() {
    Color color = getColorFromModel();
    if (Color.red.equals(color)) {
        redCrayon.setSelected(true);
    } else if (Color.yellow.equals(color)) {
        yellowCrayon.setSelected(true);
    } else if (Color.green.equals(color)) {
        greenCrayon.setSelected(true);
    } else if (Color.blue.equals(color)) {
        blueCrayon.setSelected(true);
    }
}

Check On http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html#chooserpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!