[英]Getting same color with different RGB value in Java
I am using jxl to read an Excel file. 我正在使用jxl读取Excel文件。 I need to make a decision, based on font color of a cell.
我需要根据单元格的字体颜色做出决定。 The font color of the cell, I am getting is black, but the RGB value is (1, 0, 0).
我得到的单元格的字体颜色是黑色,但是RGB值为(1、0、0)。
When I compare it with Colour.BLACK it fails the == comparison because the RGB value of Colour.BLACK is (0,0,0). 当我将其与Colour.BLACK进行比较时,==比较失败,因为Colour.BLACK的RGB值为(0,0,0)。
Colour color = nameCell.getCellFormat().getFont().getColour();
if(color == Colour.BLACK) //fails this test
options = "0";
else
options = "1";
In the above code color.getDescription()
gives black color in description. 在上面的代码中,
color.getDescription()
给出了黑色的描述。
How shall I find that "black" is common in both objects Colour.BLACK
and nameCell.getCellFormat().getFont().getColour()
? 如何找到
Colour.BLACK
和nameCell.getCellFormat().getFont().getColour()
这两个对象中常见的“黑色”?
You need a way to compare colors in Java. 您需要一种比较Java中颜色的方法。
You could find relevant information here: how could i compare colors in java? 您可以在此处找到相关信息: 如何比较Java中的颜色?
Anyway: 无论如何:
You need to compare for equality, not identity so: color == Color.BLACK
must translate to Color.BLACK.equals(color)
您需要比较相等性而不是相等性,因此:
color == Color.BLACK
必须转换为Color.BLACK.equals(color)
Since you need to compare approximately, you need a way to compute the distance between colors and force it to be under a (experimentally determined) value. 由于需要进行近似比较,因此需要一种方法来计算颜色之间的距离并将其强制为(由实验确定)的值。
Here is an example: 这是一个例子:
public static double distance (Color c1, Color c2){
double deltaRed=Math.pow((c1.getRed()-c2.getRed())/255.0,2.0);
double deltaBlue=Math.pow((c1.getBlue()-c2.getBlue())/255.0,2.0);
double deltaGreen=Math.pow((c1.getGreen()-c2.getGreen())/255.0,2.0);
double total=Math.sqrt((deltaRed+deltaBlue+deltaGreen)/3.0);
return total;
}
Color color = nameCell.getCellFormat().getFont().getColor();
if(distance(color,Color.BLACK) < 0.02)
options = "0";
else
options = "1";
rgb(1,0,0) is a very dark grey! rgb(1,0,0)是非常深的灰色!
Try 尝试
if (r <2 && g < 2 && b < 2) ....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.