Game Development Community

ColorI convolution (ColorI * ColorI)

by Orion Elenzil · 05/08/2009 (12:20 pm) · 1 comments

eg, ColorI(255, 128, 64, 255) * ColorI(128, 255, 255, 64) = 128, 128, 64, 64.

this is only worth posting because of the small gotcha of adding 255 to the product before dividing by 256. otherwise input values get shrunk by one when convolved with values of 255.

this is based on a TGE 1.3.5 codebase.

color.h
in the declaration of class ColorI, add the following after the declarations of operators *= and *:
ColorI& operator*=(const ColorI& in_mul);
   ColorI  operator*(const ColorI& in_mul) const;

then down after the definition of the * operator, ColorI ColorI::operator*(const F32 in_mul), add this:

inline ColorI& ColorI::operator*=(const ColorI& in_mul)
{
   red   = U8((U32(red  ) * U32(in_mul.red  ) + 255) >> 8);
   green = U8((U32(green) * U32(in_mul.green) + 255) >> 8);
   blue  = U8((U32(blue ) * U32(in_mul.blue ) + 255) >> 8);
   alpha = U8((U32(alpha) * U32(in_mul.alpha) + 255) >> 8);

   return *this;
}

inline ColorI ColorI::operator*(const ColorI& in_mul) const
{
   ColorI temp(*this);
   temp *= in_mul;
   return temp;
}

#1
05/10/2009 (8:18 pm)
Wow... even if I have no real plans for implementing this in my game, you have opened my eyes. I think with th same base I can implement a vertex toning function which I miss for my game.

Thanks!
Guimo