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
John Jimenez 2 weeks ago
parent
commit
e273aaea1e
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

@@ -70,6 +70,8 @@ int main(void)
             // - Define foreground color:      [cRRGGBBAA]
             // - Define foreground color:      [cRRGGBBAA]
             // - Define background color:      [bRRGGBBAA]
             // - Define background color:      [bRRGGBBAA]
             // - Reset formating:              [r]
             // - 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
             // Example: [bAA00AAFF][cFF0000FF]red text on gray background[r] normal text
 
 
             DrawTextStyled(GetFontDefault(), "This changes the [cFF0000FF]foreground color[r] of provided 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]!!!",
             DrawTextStyled(GetFontDefault(), "This changes the [c00ff00ff][bff0000ff]foreground and background colors[r]!!!",
                 (Vector2){ 100, 160 }, 20.0f, 2.0f, BLACK);
                 (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
             // Get pointer to formated text
             const char *text = TextFormat("Let's be [c%02x%02x%02xFF]CREATIVE[r] !!!", colRandom.r, colRandom.g, colRandom.b);
             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);
             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();
         EndDrawing();
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------
@@ -103,7 +108,7 @@ int main(void)
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------
 // Module Functions Definition
 // 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
 // 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)
 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
                     // Convert hex color text into actual Color
                     unsigned int colHexValue = strtoul(colHexText, NULL, 16);
                     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 ']'
                     i += (colHexCount + 1); // Skip color value retrieved and ']'
                     continue;   // Do not draw characters
                     continue;   // Do not draw characters