Java - toString to Colour

polhcujo  于 2023-03-28  发布在  Java
关注(0)|答案(6)|浏览(140)

我一整天都在想这个问题。
基本上,我已经做了一个for循环,它将条目添加到数组列表中。其中一个条目是“Colour”变量。我使用random.nextInt为颜色构造函数的红色,绿色和蓝色部分创建新值。我还设置了一个toString方法,这样我就可以看到进入数组列表的值。
问题是:
当打印出来时,我得到:java.awt.Color[r=248,g=103,b=53]
我明白为什么会这样,我只是想知道如何改变它,使输出显示“橙子”或“绿色”或“purple”,这取决于随机rgb值。
我看了这里,我发现我可能会使用getRGB方法和解析,这就是我卡住的地方...任何帮助都很好:)

rqmkfv5c

rqmkfv5c1#

您可以创建一个扩展Color并具有名为“colorName”的String的类,然后将其打印到toString上。
默认情况下,颜色不存储此名称,而且,很难通过比较这些红色,绿色,蓝色值来识别颜色。

j7dteeu8

j7dteeu82#

这是不存在的。祝你好运,滚动你自己的-它需要返回256 x 256 x 256(16,777,216)个不同的字符串之一才能工作。例如,你如何区分所有的蓝色阴影?为什么RGB值不适合你?

368yc8dk

368yc8dk3#

你可以子类化Color并覆盖toString方法(或者创建一个printColour方法或其他任何名称),然后你可以输出一个特定RGB值范围的特定颜色标签。
显然,您对颜色名称的选择和指定的特定范围将完全是主观的。

ndh0cuux

ndh0cuux4#

你可以子类化Color来覆盖toString()方法,或者添加你自己的另一个方法来将RGB转换为命名颜色。或者你可以在某处使用一个静态方法来将RGB转换为命名颜色。
然而,考虑到有256 * 256 * 256 =~ 1600万种不同的RGB组合,大多数随机RGB组合都不会对应一种指定的颜色。

2ic8powd

2ic8powd5#

这是不平凡的,而且相当主观。
但是,您可能想看看http://blog.xkcd.com/2010/05/03/color-survey-results/,它试图回答这个问题。

xwmevbvl

xwmevbvl6#

我从来没有真正遇到过你所要求的东西,但是这对你有用吗?(你可能需要add more colors来满足你的需要):

public final class ColorUtil {

    static private HashMap<String,Color> KNOWNCOLORS = new HashMap<String,Color>();

    static {
        KNOWNCOLORS.put("black",     Color.black);
        KNOWNCOLORS.put("darkgray",  Color.darkGray);
        KNOWNCOLORS.put("lightgray", Color.lightGray);
        KNOWNCOLORS.put("white",     Color.white);
        KNOWNCOLORS.put("red",       Color.red);
        KNOWNCOLORS.put("blue",      Color.blue);
        KNOWNCOLORS.put("green",     Color.green);
        KNOWNCOLORS.put("pink",      Color.pink);
        KNOWNCOLORS.put("cyan",      Color.cyan);
        KNOWNCOLORS.put("purple",    Color.magenta);
        KNOWNCOLORS.put("orange",    Color.orange);
        KNOWNCOLORS.put("yellow",    Color.yellow);
        // add more here
    }

    static public String getBestColorName(Color c) {
        int dist, diff = Integer.MAX_VALUE;
        String colorName = toHex(c);
        Color c2;

        for (String name : KNOWNCOLORS.keySet()) {
            c2 = KNOWNCOLORS.get(name);
            dist = Math.abs(c.getRed() - c2.getRed())
                    + Math.abs(c.getGreen() - c2.getGreen()) 
                    + Math.abs(c.getBlue() - c2.getBlue())
                    + Math.abs(c.getAlpha() - c2.getAlpha());

            if (dist < diff) {
                diff = dist;
                colorName = name; 
            }
        }

        return colorName;
    }

    static public int getColorValue(Color c) {
        return (c.getAlpha() << 24) 
            | (c.getRed() << 16)
            | (c.getGreen() << 8) 
            | (c.getBlue()); 
    }

    static public String toHex(Color c) {
        StringBuilder b = new StringBuilder();
        if (c.getAlpha() < 255) {
            b.append(padLeft(Integer.toString(c.getAlpha(), 16), 2));
        }
        b.append(padLeft(Integer.toString(c.getRed(), 16), 2));
        b.append(padLeft(Integer.toString(c.getGreen(), 16), 2));
        b.append(padLeft(Integer.toString(c.getBlue(), 16), 2));

        return b.toString();
    }

    static private String padLeft(String s, int n) {
        return String.format("%1$#" + n + "s", s).replace(" ", "0");
    }

    private ColorUtil() {}


    static public void main(String...args) {
        Color c;

        for (String name : KNOWNCOLORS.keySet()) {
            if (!name.equals(getBestColorName(KNOWNCOLORS.get(name)))) {
                System.out.println("*** ERR!! Could not match color " + name);
            } 
        }

        for (int i=0; i<30; i++) {
            c = new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256));

            System.out.println("Color " + toHex(c) 
                 + " (" + c.getAlpha() + "," + c.getRed() + "," + c.getGreen() + "," + c.getBlue() + ")"
                 + " = " + getBestColorName(c));
        }

        System.out.println("Done!");
    }

}

样本输出:

Color e75848 (255,231,88,72) = red
Color 6f4854 (255,111,72,84) = darkgray
Color 5dc6af (255,93,198,175) = lightgray
Color 190971 (255,25,9,113) = darkgray
Color 8ef300 (255,142,243,0) = yellow
Color c2ec5b (255,194,236,91) = lightgray
Color 998794 (255,153,135,148) = lightgray
Color 328d06 (255,50,141,6) = darkgray
Color 223d03 (255,34,61,3) = darkgray
Color 51dfea (255,81,223,234) = cyan
Color baa801 (255,186,168,1) = orange
Color 8370d4 (255,131,112,212) = lightgray
Color 7766bd (255,119,102,189) = lightgray
Color 35024b (255,53,2,75) = darkgray
Color cdbfad (255,205,191,173) = lightgray
Color 5e3a21 (255,94,58,33) = darkgray
Color 11af5d (255,17,175,93) = darkgray
Color 480995 (255,72,9,149) = darkgray
Color 04adab (255,4,173,171) = cyan
Color 98641c (255,152,100,28) = darkgray
Color 3ef68c (255,62,246,140) = cyan
Color 4d091a (255,77,9,26) = darkgray
Color 56117d (255,86,17,125) = darkgray
Color 1a00ea (255,26,0,234) = blue
Color 2ffd3e (255,47,253,62) = green
Color 9a918b (255,154,145,139) = lightgray
Color 6f8f0e (255,111,143,14) = darkgray
Color 5f3e25 (255,95,62,37) = darkgray
Color 914e79 (255,145,78,121) = darkgray
Color 57f88b (255,87,248,139) = cyan
Done!

相关问题