简体   繁体   English

如何获得RGB颜色的名称?

[英]How to get the name of RGB color?

How do you find the color name of a pixel in Java? 您如何在Java中找到像素的颜色名称? I have found the RGB values of the pixel, but have no idea of how to get the name "yellow" from the pixel. 我已经找到了像素的RGB值,但是不知道如何从像素中获得“黄色”的名称。

I haven't come across a part of the Java API that lets you do that. 我还没有遇到让您做到这一点的Java API。 It is a feature you would most likely need to implement on your own, especially since not all rgb color combinations will have names. 这是您很可能需要自己实现的功能,尤其是因为并非所有的rgb颜色组合都具有名称。

One place you could start is by creating a lookup table using the rgb color values and names given on a site such as the Wikipedia list of colors page . 您可以开始的一个地方是使用rgb颜色值和站点上给出的名称(例如Wikipedia颜色列表页面)创建查找表。

However, since not every combination of rgb values will map to a defined color, you would need to come up with an algorithm to map a set of values to the closest color. 但是,由于并非rgb值的每种组合都会映射到定义的颜色,因此您需要提出一种算法,将一组值映射到最接近的颜色。 This could be something along the lines of computing the mean squared error between the rgb value for the pixel, and the rgb values for each color name. 这可能与计算像素的rgb值和每种颜色名称的rgb值之间的均方误差有关。 You could then pick the color name with the lowest score. 然后,您可以选择得分最低的颜色名称。

Here is a gist with some code (note however, it may not compile) that illustrates how a ColorNameLookup class that computes the mean square error to find the closest matching color name could work: https://gist.github.com/nightlark/6482130 这是一个包含一些代码的要点(但是请注意,它可能不会编译)说明了ColorNameLookup类如何计算均方误差以找到最接近的匹配颜色名称: https ://gist.github.com/nightlark/ 6482130

there are some standard colors in java and others can be generated using RGB or HSB constructors. Java中有一些标准颜色,其他颜色可以使用RGB或HSB构造函数生成。 there is no way of getting the color name as there may be many combinations of RGB values (256 x 256 x 256) . 无法获得颜色名称,因为可能存在RGB值的许多组合(256 x 256 x 256)。 Only few combinations have name (standard colors like white, red, yellow, black etc.) 只有少数组合具有名称(标准颜色,如白色,红色,黄色,黑色等)

I am sure, you have got the basic problem with your question. 我敢肯定,您的问题已经解决了基本问题。

If you are trying to find out the color name of any possible combination of RGB values, this will require a exhaustive storage where you already have mapped all possible RGB values to a unique color name. 如果您试图找出任何可能的RGB值组合的颜色名称,则将需要进行详尽的存储,在该存储中,您已经将所有可能的RGB值映射到唯一的颜色名称。

This means, your question cannot be answered with 100% correctness, until this map is with you. 这意味着,直到这张地图与您在一起,您的问题才能100%正确地回答。 But yes you can definitely find the color name for the colors defined in java.awt.Color class. 但是可以,您肯定可以在java.awt.Color类中找到颜色的颜色名称。

So, you can also use reflection in java to find out the list of public static final Color properties defined in the java.awt.Color and for each value of RGB you have, you can compare it across all the declared Color in java. 因此,您还可以在Java中使用反射来找出在java.awt.Color定义的公共静态最终Color属性列表,对于您拥有的每个RGB值,您都可以将其与Java中所有声明的Color进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM