Browse Source

[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
pull/5375/head
John Jimenez 4 days ago
committed by GitHub
parent
commit
e273aaea1e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 5 deletions
  1. +18
    -5
      examples/text/text_inline_styling.c

+ 18
- 5
examples/text/text_inline_styling.c View File

@ -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

Loading…
Cancel
Save