在Java中使用不同的RGB值获得相同的颜色
我正在使用jxl读取Excel文件。我需要根据单元格的字体颜色做出决定。我得到的单元格的字体颜色是黑色,但RGB值是(1,0,0) 当我将其与color.BLACK进行比较时,由于color.BLACK的RGB值为(0,0,0),因此==比较失败 在上面的代码中,在Java中使用不同的RGB值获得相同的颜色,java,swing,jxl,Java,Swing,Jxl,我正在使用jxl读取Excel文件。我需要根据单元格的字体颜色做出决定。我得到的单元格的字体颜色是黑色,但RGB值是(1,0,0) 当我将其与color.BLACK进行比较时,由于color.BLACK的RGB值为(0,0,0),因此==比较失败 在上面的代码中,color.getDescription()在描述中给出黑色 如何发现“黑色”在对象color.black和nameCell.getCellFormat().getFont().getcolor()中都很常见?您需要一种在Java中比较
color.getDescription()
在描述中给出黑色
如何发现“黑色”在对象
color.black
和nameCell.getCellFormat().getFont().getcolor()
中都很常见?您需要一种在Java中比较颜色的方法
您可以在此处找到相关信息:
无论如何:
color==color.BLACK
必须转换为color.BLACK.equals(color)
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";
公共静态双距离(颜色c1,颜色c2){
double-deltaRed=Math.pow((c1.getRed()-c2.getRed())/255.0,2.0);
double deltable=Math.pow((c1.getBlue()-c2.getBlue())/255.0,2.0);
double deltaGreen=Math.pow((c1.getGreen()-c2.getGreen())/255.0,2.0);
双倍总计=数学sqrt((deltaRed+Deltable+deltaGreen)/3.0);
返回总数;
}
Color Color=nameCell.getCellFormat().getFont().getColor();
if(距离(颜色、颜色、黑色)<0.02)
选项=“0”;
其他的
选项=“1”;
rgb(1,0,0)是一种非常深的灰色
试一试
if(rif(color==color.BLACK)//未通过此测试
将不会编译..为了更快获得更好的帮助,请发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。顺便说一句-将对象与.equals(..)进行比较
而不是==
。可能是@andrewhompson的重复。此代码段中没有编译错误。将其包装在一个antire MCVE中,包括公共静态void main…,再加上一个用于拉入jxl的maven描述符,会使问题变得不清楚。另外,您在注释中给出了答案:)
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";
if (r <2 && g < 2 && b < 2) ....