Extracting RGB values from AGS color... [SOLVED]

Started by DoorKnobHandle, Sat 25/12/2010 23:30:37

Previous topic - Next topic

DoorKnobHandle

Hey there, I'm 99% sure I've seen a code snippet around that did this (semi-) elegantly at least, but I can't find it for the life of me.

I call DrawingSurface.GetPixel () and want to retrieve the seperate RGB values from the color value it gives me. In other words, I'd need the reverse of the Game.GetColorFromRGB () function.

Any insight?

tzachs

#1
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=40845.msg540055#msg540055

Quote from: tzachs on Sat 08/05/2010 20:42:39
Get R/G/B values from an AGS color:
Code: ags

int GetRFromColor(int color)
{
  float floatColor = IntToFloat(color);
  int result = FloatToInt(floatColor / 2048.0) * 8;  
  return result;
}

int GetGFromColor(int color)
{
  float floatColor = IntToFloat(color);
  int result = FloatToInt(( floatColor - IntToFloat(FloatToInt(floatColor / 2048.0) * 2048)) / 64.0) * 8;  
  return result;
}

int GetBFromColor(int color)
{  
  float floatColor = IntToFloat(color);
  
  float withoutR = floatColor - IntToFloat(FloatToInt(floatColor / 2048.0) * 2048);
  int withoutRInt = FloatToInt(withoutR);
  float withoutG = withoutR - IntToFloat(FloatToInt(withoutR / 64.0) * 64); 
  int withoutGInt = FloatToInt(withoutG);
  int result = withoutGInt * 8;
  if (result > 255)
  {
    result = (withoutGInt - 31) * 8 - 1;
  }
    
  return result;  
}


DoorKnobHandle

Works, awesome. Thanks! And now that this thread exists with the link and all, others will be able to find these functions much more easily!

Monsieur OUXX

#3
Hey, I'm digging up this post because I'm looking for the answer and I feel like it's incomplete.
tzachs' solution might be working, but it's not very good performance-wise, and we need a fast function for the Kart project.

Isn't there a way to achieve this using the bitwise operators?

I wrote this :
Code: ags

            int r = (color & (255 << 24)) >> 24;
            int g = (color & (255 << 16)) >> 16;
            int b = (color & (255 << 8)) >> 8;


Unforntunately it's not correct. How are the RGB values computed in an AGS 32-bits game?

Is it like this : RRRRGGGGBBBBAAAA
Or maybe like this : RRRRGGGGBBBBAAA
Or something else?

I'm too stupid to figure it out from tzachs' code, based on divisions :'-(
 

Monsieur OUXX

#4
I found the answer in the source of module TruBlu (by Monkey):

Code: ags

  // HIGHBIT  is optional and sets whether the values will be offset to the high end of the possible values.
  //This is due to the loss of precision in the conversion from RGB to an AGS color value.

  bool highBit = true; //or false, you decide

  if (color > 65535) color -= 65536;
  int rgb[ ] = new int[3];
  rgb[0] = ((color >> 11) & 31) << 3;
  rgb[1] = ((color >> 6) & 31) << 3;
  rgb[2] = (color & 31) << 3;
  if (highBit)
  {
    rgb[0] = rgb[0] | 7;
    rgb[1] = rgb[1] | 3;
    rgb[2] = rgb[2] | 7;
  }


That's a good start. I'll let everybody find out by themselves what parts of the code above are optional for it to work.
 

SMF spam blocked by CleanTalk