Allegro.cc Forums » Programming Questions » Saturation blender

Credits go to Elias for helping out!
 Elias Member #358 May 2000 Yes, the whole GLSL fragment shader would be something like this:```uniform sampler2D tex; varying vec2 uv; void main() { vec4 c = texture2D(tex, uv); float s = (c.r + c.g c.b) / 3; gl_FragColor = vec4(s, s, s, c.a); } ``` With probably a more sophisticated formula to get the saturation --"Either help out or stop whining" - Evert
 Winfield Member #15,244 July 2013 The cheap method of returning saturation for an RGB value looks something like:```function SaturationFromRGB(unsigned char r, unsigned char g, unsigned char b){ int Minimum = min(b,min(r,g)); int Maximum = max(b,max(r,g)); int Saturation = Minimum-Maximum; return Saturation; } ``` And the cheap(ish) way of applying a saturation value to something in the RGB colorspace is:```void SaturationToRGB(unsigned char *r, unsigned char *g, unsigned char *b, char Saturation){ double Pr = .299; double Pg = .587; double Pb = .114; double P=sqrt( (*r)*(*r)*Pr+ (*g)*(*g)*Pg+ (*b)*(*b)*Pb ) ; *r=P+((*r)-P)*change; *g=P+((*g)-P)*change; *b=P+((*b)-P)*change; } ``` So ultimately, the algo I'll be using when I scan through on a per-pixel basis will be something like:```unsigned char sr, sg, sb, ar, ag, ab; for (i = 0; i < sizeof(SCREEN_W); i++) { //only rendering pixels in the viewport for (j = 0; j < sizeof(SCREEN_H); j++) { PixelToBuffer(*SourceCanvas, ViewportOffsetX+i, ViewportOffsetY+j, &sr, &sg, &sb); PixelToBuffer(*AdjustCanvas, ViewportOffsetX+i, ViewportOffsetY+j, &ar, &ag, &ab); SaturationToRGB(&ar, &ag, &ab, SaturationFromRGB(sr, sg, sb)); } } ``` For now the project's strictly 2D, but I'll look into making GLSL do what I want - it'd be nice to tilt the base layer of the tilemap away from the camera slightly like Diablo 2: Lord of Destruction did. We'll see. In any case, thanks for thinking about the problem. And please forgive any mistakes, I've had to write PHP all morning and it takes a few hours for my brain to recover.;D
 Vanneto Member #8,643 May 2007 Just use tags next time.void heyThere (){}Becomes:```void heyThere () { } ``` In capitalist America bank robs you.