#include "thimg.h"
 
thimg::thimg()
{
	trtcolor.setRed(0);
	trtcolor.setGreen(0);
	trtcolor.setBlue(0);
	trtcolor.setAlpha(0);
	lumi = 0;
	cont = 0;
	rouge = 0;
	vert = 0;
	bleu = 0;
	valueadd_R = 0;
	valueadd_V = 0;
	valueadd_B = 0;
	wlimg = wlimg.convertToFormat(QImage::Format_ARGB32_Premultiplied, Qt::AutoColor);
}

void thimg::run()
{
	for (int y = 0; y < wlimg.height(); y++)
	{
		for (int x = 0; x < wlimg.width(); x++)
		{
			srcPixel = wlimg.pixel(x, y);
			trtcolor = srcPixel;
			//
			if (qAlpha(srcPixel) > 10 )
			{
				if ( cont < 0 )
				{
					ac_r = ( (float)((float)(c_r - trtcolor.red() ) / 100) * -cont);
					ac_v = ( (float)((float)(c_v - trtcolor.green() ) / 100) * -cont);
					ac_b = ( (float)((float)(c_b - trtcolor.blue() ) / 100) * -cont);
				}
				else
				{
					if ( cont > 0 )
					{
						if ( trtcolor.red() < c_r )
						{
							ac_r = ( (float)((float)(0 - trtcolor.red() ) / 100) * -cont);
						}
						else
						{
							ac_r = ( (float)((float)(0 - (((trtcolor.red() + trtcolor.green() + trtcolor.blue())/3)) ) / 100) * -cont);
						}

						if ( trtcolor.green() < c_v )
						{
							ac_v = ( (float)((float)(0 - trtcolor.green() ) / 100) * -cont);
						}
						else
						{
							ac_v = ( (float)((float)(0 - (((trtcolor.red() + trtcolor.green() + trtcolor.blue())/3)) ) / 100) * -cont);
						}

						if ( trtcolor.blue() < c_b )
						{
							ac_b = ( (float)((float)(0 - trtcolor.blue() ) / 100) * -cont);
						}
						else
						{
							ac_b = ( (float)((float)(0 - (((trtcolor.red() + trtcolor.green() + trtcolor.blue())/3)) ) / 100) * -cont);
						}
					}
					else
					{
						ac_r = 0;
						ac_v = 0;
						ac_b = 0;
					}
				}

				//
				valueadd_R = lumi + rouge + ac_r;
				valueadd_V = lumi + vert + ac_v;
				valueadd_B = lumi + bleu + ac_b;
				//

				if ( valueadd_R > 0 )
				{
					if ( trtcolor.red() < 255 - valueadd_R)
					{
						trtcolor.setRed(trtcolor.red() + valueadd_R);
					}
					else
					{
						trtcolor.setRed(255);
					}
				}
				else
				{
					if ( trtcolor.red() > (valueadd_R*-1))
					{
						trtcolor.setRed(trtcolor.red() + valueadd_R);
					}
					else
					{
						trtcolor.setRed(0);
					}
				}

				if ( valueadd_V > 0 )
				{
					if ( trtcolor.green() < 255 - valueadd_V)
					{
						trtcolor.setGreen(trtcolor.green() + valueadd_V);
					}
					else
					{
						trtcolor.setGreen(255);
					}
				}
				else
				{
					if ( trtcolor.green() > (valueadd_V*-1))
					{
						trtcolor.setGreen(trtcolor.green() + valueadd_V);
					}
					else
					{
						trtcolor.setGreen(0);
					}
				}

				if ( valueadd_B > 0 )
				{
					if ( trtcolor.blue() < 255 - valueadd_B)
					{
						trtcolor.setBlue(trtcolor.blue() + valueadd_B);
					}
					else
					{
						trtcolor.setBlue(255);
					}
				}
				else
				{
					if ( trtcolor.blue() > (valueadd_B*-1))
					{
						trtcolor.setBlue(trtcolor.blue() + valueadd_B);
					}
					else
					{
						trtcolor.setBlue(0);
					}
				}
				//
				trtcolor.setAlpha(opacity);
				wlimg.setPixel(x, y, trtcolor.rgba());
			}
		}
	}
	emit myfin();
	//exec();
}

