so at lesson 13,14 i've decided to make a tetris game using square draw function and line drawing.
the problem was that i wanted the edges of the square to have like a shadow on the right and bottom side and an brighter area in the top and left side .
so i banged my head around to make a function to give any rgb combination to it and return me a brighter or darker shade of that color.
first i tried to just increase/decrease rgb values each by 10 and that wasn't the solution,got me diferent collor's all time.
then i studied in paint and tought that to keep the same collor shade the rgb value must keep they'r ratio with eachother so i've spend like a day trying to find a solution.
in the end i just realised the rgb value changes must be in relation with the max value for increasing and the min value for decreasing.
so after more fine tuning i got with a code that works:D
i 'm curios if this can be simplified anymore,basicaly all it does is changing the rgb values in a way that if u add they will all end up at 255 at same time or at 0 at same time if u substract.
anyway i tought it would be a good idea to share this code since it took me that long to figure it out:D
Code: Select all
int D3DGraphics::ChangeBrightness(int r,int g,int b,int factor,int retval)// the rgb values,ammount to change and the return value can't return all 3 vallues at once sadly:(
{
float r1=r;
float g1=g;
float b1=b;
float r2=r;
float g2=g;
float b2=b;
float ratio;
if(factor>0)
{
//adding to get a brighter shade of the collor
ratio=255-r1;
r1=r1+((factor*ratio)/255);
ratio=255-g1;
g1=g1+((factor*ratio)/255);
ratio=255-b1;
b1=b1+((factor*ratio)/255);
//returning whateevr vallue was requested
if (retval==1)
{
return r1;
}
if (retval==2)
{
return g1;
}
if (retval==3)
{
return b1;
}
}
else
{
//substracting to get a darker shade of the collor
//as u notice the factor for substracting is the actual collor value,that's because the value till 0 is needed for substracting,while for adding u need the value from collor number to 255
r2=r2-((-factor*r2)/255);
g2=g2-((-factor*g1)/255);
b2=b2-((-factor*b2)/255);
//returning whateevr vallue was requested
if (retval==1)
{
return r2;
}
if (retval==2)
{
return g2;
}
if (retval==3)
{
return b2;
}
}
}