#include "thimg.h"
 
thimg::thimg()
{
	trtcolor.setRed(0);
	trtcolor.setGreen(0);
	trtcolor.setBlue(0);
	ouverture = 0;
	hasover = 0;
	hasinvert = 0;
}

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);
					}
				}
				//
				wlimg.setPixel(x, y, trtcolor.rgb());
			}
		}
	}
	if ( ouverture > 0 )
	{
		ouvcolor.setAlpha(opacity);
		QPainter::CompositionMode mode = (QPainter::CompositionMode)composition;
		QRadialGradient mygradient(qreal(wlimg.width()*((float)radius/(float)100)),qreal(wlimg.height()*((float)longueur/(float)100)),qreal(wlimg.width()),qreal(wlimg.width()*((float)radius/(float)100)),qreal(wlimg.height()*((float)longueur/(float)100)));
		if ( hasinvert == 0 )
		{
			mygradient.setColorAt(0.0,QColor(0,0,0,0));
			mygradient.setColorAt(qreal(1.0-((float)ouverture/(float)100)),ouvcolor);
		}
		else
		{
			mygradient.setColorAt(qreal(1.0-((float)ouverture/(float)100)),QColor(0,0,0,0));
			mygradient.setColorAt(0.0,ouvcolor);
		}
		if ( hasover == 0 )
		{
			p.begin(&wlimg);
			p.setCompositionMode ( mode );
			p.fillRect ( 0, 0, wlimg.width() ,wlimg.height(), QBrush( mygradient ) );
			p.end();
		}
		else
		{
			QImage wl_image = overpix.convertToFormat ( QImage::Format_ARGB32_Premultiplied, Qt::AutoColor );
			QPainter::CompositionMode mode2 = (QPainter::CompositionMode)6;
			p.begin(&wl_image);
			p.setCompositionMode ( mode2 );
			//p.drawImage(QRect(0,0,-1,-1),wl_image);
			p.fillRect ( 0, 0, wlimg.width() ,wlimg.height(), QBrush( mygradient ) );
			p.end();

			p.begin(&wlimg);
			p.setCompositionMode ( mode );
			p.drawImage(0,0,wl_image,0,0,-1,-1,Qt::AutoColor);
			p.end();
		}
	}
	emit myfin();
	//exec();
}

