Advertisement
p4r4digm

Gamma-Corrected color distance

Oct 31st, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. /* Return gamma-corrected value of a color component byte*/
  2. float GCRGB(byte component) {
  3.    static float GCRGBTable[256] = {0.0f};
  4.    static int loaded = 0;
  5.  
  6.    if(!loaded) {
  7.       int i;
  8.       for(i = 0; i < 256; ++i) {
  9.          GCRGBTable[i] = pow(i/255.0f, 2.2f);
  10.       }
  11.       loaded = 1;
  12.    }
  13.  
  14.    return GCRGBTable[component];
  15. }
  16.  
  17. float difference(Color c1, Color c2) {
  18.    float r, g, b;
  19.    r = GCRGB(c1.r) - GCRGB(c2.r);
  20.    g = GCRGB(c1.g) - GCRGB(c2.g);
  21.    b = GCRGB(c1.b) - GCRGB(c2.b);
  22.  
  23.  
  24.  
  25.    return r*r + g*g + b*b;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement