Skip to content

Commit 2c9d116

Browse files
committed
ADDED: ColorTint(), ColorContrast()
1 parent f1368c3 commit 2c9d116

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/raylib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,9 @@ RLAPI Vector4 ColorNormalize(Color color); // G
13311331
RLAPI Color ColorFromNormalized(Vector4 normalized); // Get Color from normalized values [0..1]
13321332
RLAPI Vector3 ColorToHSV(Color color); // Get HSV values for a Color, hue [0..360], saturation/value [0..1]
13331333
RLAPI Color ColorFromHSV(float hue, float saturation, float value); // Get a Color from HSV values, hue [0..360], saturation/value [0..1]
1334-
RLAPI Color ColorBrightness(Color color, float factor); // Get color with brightness correction, brightness factor goes from 0.0f to 1.0f
1334+
RLAPI Color ColorTint(Color color, Color tint); // Get color multiplied with another color
1335+
RLAPI Color ColorBrightness(Color color, float factor); // Get color with brightness correction, brightness factor goes from -1.0f to 1.0f
1336+
RLAPI Color ColorContrast(Color color, float contrast); // Get color with contrast correction, contrast values between -1.0f and 1.0f
13351337
RLAPI Color ColorAlpha(Color color, float alpha); // Get color with alpha applied, alpha goes from 0.0f to 1.0f
13361338
RLAPI Color ColorAlphaBlend(Color dst, Color src, Color tint); // Get src alpha-blended into dst color with tint
13371339
RLAPI Color GetColor(unsigned int hexValue); // Get Color structure from hexadecimal value

src/rtextures.c

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3944,7 +3944,30 @@ Color ColorFromHSV(float hue, float saturation, float value)
39443944
return color;
39453945
}
39463946

3947-
// Get color with brightness correction, brightness factor goes from 0.0f to 1.0f
3947+
// Get color multiplied with another color
3948+
Color ColorTint(Color color, Color tint)
3949+
{
3950+
Color result = color;
3951+
3952+
float cR = (float)tint.r/255;
3953+
float cG = (float)tint.g/255;
3954+
float cB = (float)tint.b/255;
3955+
float cA = (float)tint.a/255;
3956+
3957+
unsigned char r = (unsigned char)(((float)color.r/255*cR)*255.0f);
3958+
unsigned char g = (unsigned char)(((float)color.g/255*cG)*255.0f);
3959+
unsigned char b = (unsigned char)(((float)color.b/255*cB)*255.0f);
3960+
unsigned char a = (unsigned char)(((float)color.a/255*cA)*255.0f);
3961+
3962+
result.r = r;
3963+
result.g = g;
3964+
result.b = b;
3965+
result.a = a;
3966+
3967+
return result;
3968+
}
3969+
3970+
// Get color with brightness correction, brightness factor goes from -1.0f to 1.0f
39483971
Color ColorBrightness(Color color, float factor)
39493972
{
39503973
Color result = color;
@@ -3977,6 +4000,49 @@ Color ColorBrightness(Color color, float factor)
39774000
return result;
39784001
}
39794002

4003+
// Get color with contrast correction
4004+
// NOTE: Contrast values between -1.0f and 1.0f
4005+
Color ColorContrast(Color color, float contrast)
4006+
{
4007+
Color result = color;
4008+
4009+
if (contrast < -1.0f) contrast = -1.0f;
4010+
else if (contrast > 1.0f) contrast = 1.0f;
4011+
4012+
contrast = (1.0f + contrast);
4013+
contrast *= contrast;
4014+
4015+
float pR = (float)color.r/255.0f;
4016+
pR -= 0.5f;
4017+
pR *= contrast;
4018+
pR += 0.5f;
4019+
pR *= 255;
4020+
if (pR < 0) pR = 0;
4021+
else if (pR > 255) pR = 255;
4022+
4023+
float pG = (float)color.g/255.0f;
4024+
pG -= 0.5f;
4025+
pG *= contrast;
4026+
pG += 0.5f;
4027+
pG *= 255;
4028+
if (pG < 0) pG = 0;
4029+
else if (pG > 255) pG = 255;
4030+
4031+
float pB = (float)color.b/255.0f;
4032+
pB -= 0.5f;
4033+
pB *= contrast;
4034+
pB += 0.5f;
4035+
pB *= 255;
4036+
if (pB < 0) pB = 0;
4037+
else if (pB > 255) pB = 255;
4038+
4039+
result.r = (unsigned char)pR;
4040+
result.g = (unsigned char)pG;
4041+
result.b = (unsigned char)pB;
4042+
4043+
return result;
4044+
}
4045+
39804046
// Get color with alpha applied, alpha goes from 0.0f to 1.0f
39814047
Color ColorAlpha(Color color, float alpha)
39824048
{

0 commit comments

Comments
 (0)