[英]How to make rang for color in java?
I want to type a 6-digit-number (RGB-hex) and then click on a button to show the color's name. 我想输入一个6位数字(RGB-hex),然后单击一个按钮以显示颜色的名称。 I did not find color lib in java to give me arbitrary colors' names. 我在Java中找不到color lib来给我任意颜色的名称。
So what I do is : I found this lib in javascript this is the ntc js lib and tried to re-implement it in java , so I defined a hash map and put each value with the name for example : 所以我要做的是:我在javascript中找到了这个库, 它是ntc js库,并试图在Java中重新实现它,因此我定义了一个哈希映射,并在每个值中都加上了名称,例如:
sColorNameMap.put("B43332", "Well Read");
The code runs ok when given the same key which I put in the hash map, for example when I write the value B43332 then I get Well Read as name of color. 当给定与哈希映射中相同的键时,代码可以正常运行,例如,当我写入值B43332时,我会得到Well Read作为颜色的名称。
My problem: When I enter a value not equal to the key I pushed then I can not get the name of color. 我的问题:当我输入的值不等于我按下的键时,我将无法获得颜色的名称。
What I need : I need a function to return the name of the closest matching color. 我需要的是:我需要一个函数来返回最接近的匹配颜色的名称。 I tried to understand how ntc does it but I couldn't. 我试图了解ntc是如何做到的,但我做不到。
For example the value B23634
is not pre-defined in ntc js lib BUT it resolves to give the name Well Read , like this picture : 例如,值B23634
不是在ntc js lib中预定义的,但是它解析为名称Well Read ,如下图所示: So any help to implement a function returning the closest value (6 digit of number) to a certain color is appreciated. 因此,对实现将最接近的值(数字的6位数字)返回某种颜色的功能的任何帮助表示赞赏。
If you consider the closest matching color as the one closest in the 3 dimensional RGB space, you can use something like this: 如果您将最接近的匹配颜色视为3维RGB空间中最接近的颜色 ,则可以使用以下方法:
(check this for information on how to calculate that distance) (检查此信息以获取有关如何计算该距离的信息)
public static void main(String[] args) {
Map<String, String> sColorNameMap = new HashMap<>();
String rgbCode = "B23634";
String colorName;
if (sColorNameMap.containsKey(rgbCode)) {
colorName = sColorNameMap.get(rgbCode);
} else {
colorName = nearestColor(rgbCode, sColorNameMap);
}
System.out.println("colorName = " + colorName);
}
private static String nearestColor(String code, Map<String, String> sColorNameMap) {
int[] rgb = getRgb(code);
double nearestDistance = Double.MAX_VALUE;
String nearestNamedColorCode = null;
for (String namedColorCode : sColorNameMap.keySet()) {
int[] namedColorRgb = getRgb(namedColorCode);
double distance = calculateDistance(rgb, namedColorRgb);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestNamedColorCode = namedColorCode;
}
}
return sColorNameMap.get(nearestNamedColorCode);
}
private static int[] getRgb(String code) {
int r = Integer.parseInt(code.substring(0, 2), 16);
int g = Integer.parseInt(code.substring(2, 4), 16);
int b = Integer.parseInt(code.substring(4, 6), 16);
return new int[]{r, g, b};
}
private static double calculateDistance(int[] rgb1, int[] rgb2) {
double sum = 0.0;
for (int i = 0; i < 3; i++) {
sum += Math.pow(rgb2[i] - rgb1[i], 2);
}
return Math.sqrt(sum);
}
If you have some other metric for closest matching color , it is just the matter of changing the implementation in calculateDistance()
method. 如果您还有其他用于最接近匹配颜色的度量标准,则只需更改calculateDistance()
方法中的实现即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.