From f1368c36dd38dd912782485ce827fb8b35b86365 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 5 Dec 2022 00:24:55 +0100 Subject: [PATCH] ADDED: `ColorBrightness()` --- src/raylib.h | 1 + src/rtextures.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index c48380d82..6642bc8d3 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1331,6 +1331,7 @@ 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 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 82de04785..66434c116 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -3944,6 +3944,39 @@ Color ColorFromHSV(float hue, float saturation, float value) return color; } +// Get color with brightness correction, brightness factor goes from 0.0f to 1.0f +Color ColorBrightness(Color color, float factor) +{ + Color result = color; + + if (factor > 1.0f) factor = 1.0f; + else if (factor < -1.0f) factor = -1.0f; + + float red = (float)color.r; + float green = (float)color.g; + float blue = (float)color.b; + + if (factor < 0.0f) + { + factor = 1.0f + factor; + red *= factor; + green *= factor; + blue *= factor; + } + else + { + red = (255 - red)*factor + red; + green = (255 - green)*factor + green; + blue = (255 - blue)*factor + blue; + } + + result.r = (unsigned char)red; + result.g = (unsigned char)green; + result.b = (unsigned char)blue; + + return result; +} + // Get color with alpha applied, alpha goes from 0.0f to 1.0f Color ColorAlpha(Color color, float alpha) {