Click here to Skip to main content
15,911,327 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some RGB color value like #e03cec and I want to make it to int array ,I need some help.
I have write code like this:
C#
public static int [] TestInt(String rgbColor)
{
    String rgb = rgbColor.substring(1, rgbColor.length());
    String [] Hex= new String [3];
    int [] IntRgb =new int [3];
    for(int i=0;i<3;i++)
    {
      Hex[i]=rgb.substring(2*i,2*i+2);
      IntRgb[i]=Integer.parseInt(Hex[i],16);
    }
    return IntRgb;
}

but I can not make sure whether it is right.
Posted
Updated 26-Nov-13 0:55am
v2
Comments
Richard MacCutchan 26-Nov-13 7:15am    
Please explain what problems you get with this code.
wf0007 26-Nov-13 8:12am    
I use this int array to convert to HSL(Hue, Saturation, Lightness), I find there are display some error value, so I need to confirm whether this method is right.
Richard MacCutchan 26-Nov-13 8:43am    
I find there are display some error value
Unless you explain the actual error you receive we cannot offer any suggestions. Please do not expect us to guess what happens in your code.

1 solution

It is actually very easy to check. Write a test :)


So, you want to test if your method correctly converts the input RGB string into a 3 element integer array.

The sequence you gave should be split into the following three elements

R = e0, which is 0xe0
G = 3c, which is 0x3c
B = ec, which is 0xec

So to test, I would sucggest you use a unit testing framework like junit, but below is a quick and dirty test snippet, to explain the principle.

Java
int[] result = TestInt("#e03cec");

// So because it is integer value, you can compare the hex values
// expected to the array
if (result[0] != 0xe0) throw new RuntimeException("Error");
if (result[1] != 0x3c) throw new RuntimeException("Error");
if (result[2] != 0xec) throw new RuntimeException("Error");

// (or if you are really paranoid you can use
// a calculator to convert the hex to base 10 (int) which would be
// 224, 60 and 236
if (result[0] != 224) throw new RuntimeException("Error");
if (result[1] != 60) throw new RuntimeException("Error");
if (result[2] != 236) throw new RuntimeException("Error");

System.out.println("OK");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900