• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

RGB Color calculations

Red Squirrel

No Lifer
I'm writing a png library wrapper to make png image generation easier and adding various features like color addition, adding opacity etc... Just wondering if I'm on the right track.

Basically there's different modifiers that can be added before a pixel write operation is done. There's opacity, which will make it so that whatever I write is semi transparant and takes the colors of the existing pixels into account. this is not to be confused with png alpha, that's not what this is. Basically equivalent to adding transparancy to a layer in photoshop.

Then there's addition and substraction, I think I got those down path, if it's really as simple as I think it is.

The results I'm getting SEEM right, here is the code with calculations:

Code:
	//writes a single pixel to memory
	void rspng::WritePixel(int x,int y, int red, int green, int blue, int alpha=255)
	{
		if(m_width==0 || m_height==0 || m_imagedata==NULL)return; //size not determined, we just bail		
		if (x>m_width || y>m_height) return; //out of range, just bail
		
		//fix within proper range:  
		if(red>255)red=255;
		if(red<0)red=0;
		if(green>255)green=255;
		if(green<0)green=0;
		if(blue>255)blue=255;
		if(blue<0)blue=0;
		if(alpha>255)alpha=255;
		if(alpha<0)alpha=0;
		
		//debug msg
		if(DebugLevel(5))
		{
			stringstream ss;
			ss<<"Write ("<<red<<","<<green<<","<<blue<<","<<alpha<<") to ("<<x<<","<<y<<")...";
			Debug(5,ss.str());
		}		
		
		//get the existing pixel:
		
		pngpixel * pixel = GetPixel(x,y);
		if(pixel==NULL) return; //this should not happen because of previous checks...
		
					
		//temp variables to store final color
		int redtw=red;
		int greentw=green;
		int bluetw=blue;
		int alphatw=alpha;
		
		//existing colors:
		int redc=pixel->GetRed();
		int greenc=pixel->GetGreen();
		int bluec=pixel->GetBlue();
		int alphac=pixel->GetAlpha();

		
		float opacity=(float)mod_opacity_perc/100; //convert to decimal to make rest of calculations easier
						 
		//check modifiers and make any color calculations:
		
		
		if(mod_addition)
		{
			redtw=(red*opacity)+redc;
			greentw=(green*opacity)+greenc;
			bluetw=(blue*opacity)+bluec;
		}
		
		if(mod_substraction)
		{
			redtw=redc-(red*opacity);
			greentw=greenc-(green*opacity);
			bluetw=bluec-(blue*opacity);
		}
		
		
		//if no mods were applied, do a regular write taking opacity into account unless opacity is 100 then we just skip this part (tw vars already have function sent values)
		if(mod_opacity_perc<100 && !mod_addition && !mod_substraction)
		{
			float opacityrev = 1-opacity;
			
			redtw=(redc*opacityrev)+(red*opacity);
			greentw=(greenc*opacityrev)+(green*opacity);
			bluetw=(bluec*opacityrev)+(blue*opacity);
		}
			
		
		
		 
		//fix within proper range:  (again in case any transformations happened)
		if(redtw>255)redtw=255;
		if(redtw<0)redtw=0;
		if(greentw>255)greentw=255;
		if(greentw<0)greentw=0;
		if(bluetw>255)bluetw=255;
		if(bluetw<0)bluetw=0;
		if(alphatw>255)alphatw=255;
		if(alphatw<0)alphatw=0;
						 
		pixel->SetColor((unsigned char)redtw,(unsigned char)greentw,(unsigned char)bluetw,(unsigned char)alphatw);		
	}




I'm also open to other fun things I can maybe add as modifiers. Basically these are just bool variables that I can apply before a function is called such as plotting a rectangle. By default it will just fully overwrite the pixels.
 
Have you thought about possibly converting from RBG to HSI, process image, then convert back from HSI to RBG? Adding the conversion should open up more image processing techniques to perhaps include. I know it's probably more than intended for this project, but it was just a thought.
 
Well most things work in RGB, I'm just adding features to add color transformations so just need the formulas.

I got additive and substractive down path I think, was just comparing with the RGB values in gimp when I do the same operations to two layers.

I can't seem to figure out what happens during division and multiplication though. Worse case I might just leave those out, I don't really see myself using those. Even addition and substraction. Most of the time if I want to programatically add pixel data I'll just add it on top, I might add opacity to it so that it blends with existing pixels, but I got that part down path too.
 
Back
Top