0

i want to generate RGB to CMYK value using this code

    pixel = temp.getPixel(i,j);
    R = Color.red(pixel);
    G = Color.green(pixel);
    B = Color.blue(pixel);
    K = Math.min(Math.min(255-R, 255-G), 255-B);
    if (K!=255){
        c = ((255-R-K)/(255-K));
        m = ((255-G-K)/(255-K));
        y = ((255-B-K)/(255-K));
        C = (int)(255*c);
        M = (int)(255*m);
        Y = (int)(255*y);
    } else {
        C = 255-R;
        M = 255-G;
        Y = 255-B;
    }

The type of variabel pixel,R,G,B,K,C,M and Y are Integer. and the type of variabel c, m, and y are float. but when I show the result to the log cat like this,

Log.i("CMYK", String.valueOf(C)+" "+String.valueOf(M)+" "+String.valueOf(Y)+" "+String.valueOf(K));
Log.i("CMYK", String.valueOf(c)+" = ((255-"+String.valueOf(R)+"-"+String.valueOf(K)+")/(255-"+String.valueOf(K)+"))");
Log.i("CMYK", String.valueOf(m)+" = ((255-"+String.valueOf(G)+"-"+String.valueOf(K)+")/(255-"+String.valueOf(K)+"))");
Log.i("CMYK", String.valueOf(y)+" = ((255-"+String.valueOf(B)+"-"+String.valueOf(K)+")/(255-"+String.valueOf(K)+"))");

they give me this result of the log cat :

08-18 18:34:49.080: I/CMYK(819): 0 0 0 142
08-18 18:34:49.080: I/CMYK(819): 0.0 = ((255-90-142)/(255-142))
08-18 18:34:49.080: I/CMYK(819): 0.0 = ((255-113-142)/(255-142))
08-18 18:34:49.090: I/CMYK(819): 0.0 = ((255-99-142)/(255-142))

Just like the log say, value of R = 90, G = 113, and B = 99... can anyone explain me why the mathematic result are 0??

adi.zean
  • 1,085
  • 3
  • 12
  • 15
  • Please print the initial values of RGB in logs too, i think that the values of R, G, and B are already `0`. – Salman Khakwani Aug 18 '13 at 18:42
  • 1
    When you start getting 0 as a result of integer math, it's usually because a divisor in an expression is larger than the dividend. `int x=1/5 => 0`, `int y=1/1.2 => 0` whereas `double z=1/1/2 => 0.833333` – depwl9992 Aug 18 '13 at 18:43
  • @Salman Khakwani : the R = 90, G = 113, and B = 99.. its show at the log – adi.zean Aug 18 '13 at 19:51
  • @depwl9992 yeah i know that, thats why I set the small c,m,y as float and the caps C,M,Y,K are integer... but its still give 0 for the result – adi.zean Aug 18 '13 at 19:53

1 Answers1

0

If you use Integers then:

255-90-142 = 23
255-142 = 113
23 / 113 = 0   <-- becouse it's int

Same for rest of your code.

M G
  • 1,240
  • 14
  • 26
  • I set the c, m, y are float. and the C,M,Y,K are int. is the result of c = ((255-90-142)/(255-142)) = (23/113) = 0,20353 (because the c is float) then the C = (int)(255*0,20353) = 51..but it give 0 at c,,, where is my mistake?? – adi.zean Aug 18 '13 at 19:49
  • 1
    @adi.zean becouse normal numbers(255, 90, 142) are Ints so ((255-90-142)/(255-142)) is Int. Change it to ((255f-90-142)/(255-142)) – M G Aug 18 '13 at 19:57