From e273aaea1ed575d6bc67155dbc2101868fc72669 Mon Sep 17 00:00:00 2001 From: John Jimenez Date: Sun, 30 Nov 2025 01:10:15 +0800 Subject: [PATCH] [examples] text_inline_styling: make inline text and background colors respect base alpha (#5373) * Added source alpha multiplier for text inline styling examples * Added header description about base alpha multiplier --- examples/text/text_inline_styling.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/examples/text/text_inline_styling.c b/examples/text/text_inline_styling.c index aeebe0abc..24e2704f7 100644 --- a/examples/text/text_inline_styling.c +++ b/examples/text/text_inline_styling.c @@ -70,6 +70,8 @@ int main(void) // - Define foreground color: [cRRGGBBAA] // - Define background color: [bRRGGBBAA] // - Reset formating: [r] + // Colors defined with [cRRGGBBAA] or [bRRGGBBAA] are multiplied by the base color alpha + // This allows global transparency control while keeping per-section styling (ex. text fade effects) // Example: [bAA00AAFF][cFF0000FF]red text on gray background[r] normal text DrawTextStyled(GetFontDefault(), "This changes the [cFF0000FF]foreground color[r] of provided text!!!", @@ -81,12 +83,15 @@ int main(void) DrawTextStyled(GetFontDefault(), "This changes the [c00ff00ff][bff0000ff]foreground and background colors[r]!!!", (Vector2){ 100, 160 }, 20.0f, 2.0f, BLACK); + DrawTextStyled(GetFontDefault(), "This changes the [c00ff00ff]alpha[r] relative [cffffffff][b000000ff]from source[r] [cff000088]color[r]!!!", + (Vector2){ 100, 200 }, 20.0f, 2.0f, (Color){ 0, 0, 0, 100 }); + // Get pointer to formated text const char *text = TextFormat("Let's be [c%02x%02x%02xFF]CREATIVE[r] !!!", colRandom.r, colRandom.g, colRandom.b); - DrawTextStyled(GetFontDefault(), text, (Vector2){ 100, 220 }, 40.0f, 2.0f, BLACK); + DrawTextStyled(GetFontDefault(), text, (Vector2){ 100, 240 }, 40.0f, 2.0f, BLACK); textSize = MeasureTextStyled(GetFontDefault(), text, 40.0f, 2.0f); - DrawRectangleLines(100, 220, (int)textSize.x, (int)textSize.y, GREEN); + DrawRectangleLines(100, 240, (int)textSize.x, (int)textSize.y, GREEN); EndDrawing(); //---------------------------------------------------------------------------------- @@ -103,7 +108,7 @@ int main(void) //---------------------------------------------------------------------------------- // Module Functions Definition //---------------------------------------------------------------------------------- -// Draw text using inline styling +// Draw text using inline styling, using input color as the base alpha multiplied to inline styles // PARAM: color is the default text color, background color is BLANK by default static void DrawTextStyled(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color color) { @@ -171,8 +176,16 @@ static void DrawTextStyled(Font font, const char *text, Vector2 position, float // Convert hex color text into actual Color unsigned int colHexValue = strtoul(colHexText, NULL, 16); - if (text[i - 1] == 'c') colFront = GetColor(colHexValue); - else if (text[i - 1] == 'b') colBack = GetColor(colHexValue); + if (text[i - 1] == 'c') + { + colFront = GetColor(colHexValue); + colFront.a *= (float)color.a / 255.0f; + } + else if (text[i - 1] == 'b') + { + colBack = GetColor(colHexValue); + colBack.a *= (float)color.a / 255.0f; + } i += (colHexCount + 1); // Skip color value retrieved and ']' continue; // Do not draw characters