-1

I am using jxl to read an Excel file. 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).

When I compare it with Colour.BLACK it fails the == comparison because the RGB value of Colour.BLACK is (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.

How shall I find that "black" is common in both objects Colour.BLACK and nameCell.getCellFormat().getFont().getColour() ?

Mysterion
  • 9,050
  • 3
  • 30
  • 52
Mohamed Iqzas
  • 976
  • 1
  • 14
  • 19
  • `if(color ==Colour.BLACK)//fails this test` Would not compile.. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). BTW - compare objects with `.equals(..)` rather than `==` .. – Andrew Thompson Jan 28 '15 at 09:28
  • possible duplicate of [how could i compare colors in java?](http://stackoverflow.com/questions/15262258/how-could-i-compare-colors-in-java) – Catalina Island Jan 28 '15 at 12:03
  • 1
    @AndrewThompson there are no compilation errors in this snippet. Wrapping this in an antire MCVE, including the public static void main..., plus a maven descriptor to pull in jxl, would make the question unclear instead. Plus you gave the answer in your comment anyway :) – Joeri Hendrickx Jan 28 '15 at 12:34

2 Answers2

2

You need a way to compare colors in Java.

You could find relevant information here: how could i compare colors in java?

Anyway:

  1. You need to compare for equality, not identity so: color == Color.BLACK must translate to Color.BLACK.equals(color)

  2. 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";    
Community
  • 1
  • 1
Carlo Pellegrini
  • 5,656
  • 40
  • 45
0

rgb(1,0,0) is a very dark grey!

Try

if (r <2 && g < 2 && b < 2) ....
James Anderson
  • 27,109
  • 7
  • 50
  • 78