From 2c9d116a5ce835328dc3267313f1b34b2e7ad8c9 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 7 Dec 2022 12:52:42 +0100 Subject: [PATCH] ADDED: `ColorTint()`, `ColorContrast()` --- src/raylib.h | 4 ++- src/rtextures.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index 6642bc8d..dc71af84 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1331,7 +1331,9 @@ RLAPI Vector4 ColorNormalize(Color color); // G RLAPI Color ColorFromNormalized(Vector4 normalized); // Get Color from normalized values [0..1] RLAPI Vector3 ColorToHSV(Color color); // Get HSV values for a Color, hue [0..360], saturation/value [0..1] RLAPI Color ColorFromHSV(float hue, float saturation, float value); // Get a Color from HSV values, hue [0..360], saturation/value [0..1] -RLAPI Color ColorBrightness(Color color, float factor); // Get color with brightness correction, brightness factor goes from 0.0f to 1.0f +RLAPI Color ColorTint(Color color, Color tint); // Get color multiplied with another color +RLAPI Color ColorBrightness(Color color, float factor); // Get color with brightness correction, brightness factor goes from -1.0f to 1.0f +RLAPI Color ColorContrast(Color color, float contrast); // Get color with contrast correction, contrast values between -1.0f and 1.0f RLAPI Color ColorAlpha(Color color, float alpha); // Get color with alpha applied, alpha goes from 0.0f to 1.0f RLAPI Color ColorAlphaBlend(Color dst, Color src, Color tint); // Get src alpha-blended into dst color with tint RLAPI Color GetColor(unsigned int hexValue); // Get Color structure from hexadecimal value diff --git a/src/rtextures.c b/src/rtextures.c index 66434c11..3313a06f 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -3944,7 +3944,30 @@ Color ColorFromHSV(float hue, float saturation, float value) return color; } -// Get color with brightness correction, brightness factor goes from 0.0f to 1.0f +// Get color multiplied with another color +Color ColorTint(Color color, Color tint) +{ + Color result = color; + + float cR = (float)tint.r/255; + float cG = (float)tint.g/255; + float cB = (float)tint.b/255; + float cA = (float)tint.a/255; + + unsigned char r = (unsigned char)(((float)color.r/255*cR)*255.0f); + unsigned char g = (unsigned char)(((float)color.g/255*cG)*255.0f); + unsigned char b = (unsigned char)(((float)color.b/255*cB)*255.0f); + unsigned char a = (unsigned char)(((float)color.a/255*cA)*255.0f); + + result.r = r; + result.g = g; + result.b = b; + result.a = a; + + return result; +} + +// Get color with brightness correction, brightness factor goes from -1.0f to 1.0f Color ColorBrightness(Color color, float factor) { Color result = color; @@ -3977,6 +4000,49 @@ Color ColorBrightness(Color color, float factor) return result; } +// Get color with contrast correction +// NOTE: Contrast values between -1.0f and 1.0f +Color ColorContrast(Color color, float contrast) +{ + Color result = color; + + if (contrast < -1.0f) contrast = -1.0f; + else if (contrast > 1.0f) contrast = 1.0f; + + contrast = (1.0f + contrast); + contrast *= contrast; + + float pR = (float)color.r/255.0f; + pR -= 0.5f; + pR *= contrast; + pR += 0.5f; + pR *= 255; + if (pR < 0) pR = 0; + else if (pR > 255) pR = 255; + + float pG = (float)color.g/255.0f; + pG -= 0.5f; + pG *= contrast; + pG += 0.5f; + pG *= 255; + if (pG < 0) pG = 0; + else if (pG > 255) pG = 255; + + float pB = (float)color.b/255.0f; + pB -= 0.5f; + pB *= contrast; + pB += 0.5f; + pB *= 255; + if (pB < 0) pB = 0; + else if (pB > 255) pB = 255; + + result.r = (unsigned char)pR; + result.g = (unsigned char)pG; + result.b = (unsigned char)pB; + + return result; +} + // Get color with alpha applied, alpha goes from 0.0f to 1.0f Color ColorAlpha(Color color, float alpha) {