diff --git a/examples/shapes/shapes_rectangle_scaling.c b/examples/shapes/shapes_rectangle_scaling.c index 6940cbcd0..3e5c07424 100644 --- a/examples/shapes/shapes_rectangle_scaling.c +++ b/examples/shapes/shapes_rectangle_scaling.c @@ -12,6 +12,7 @@ ********************************************************************************************/ #include "raylib.h" +#include #define MOUSE_SCALE_MARK_SIZE 12 @@ -31,6 +32,11 @@ int main(void) bool mouseScaleReady = false; bool mouseScaleMode = false; + Color from = LIME; + Color to = GREEN; + Color color = from; + float t = 0.0f; + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -39,6 +45,8 @@ int main(void) { // Update //---------------------------------------------------------------------------------- + double time = GetTime(); + mousePosition = GetMousePosition(); if (CheckCollisionPointRec(mousePosition, rec) && @@ -59,6 +67,9 @@ int main(void) if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE; if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE; + t = 0.5f * sin(time * 4.0f) + 0.5f; + color = FadeBetween(from, to, t); + if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) mouseScaleMode = false; } //---------------------------------------------------------------------------------- @@ -71,7 +82,7 @@ int main(void) DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY); - DrawRectangleRec(rec, Fade(GREEN, 0.5f)); + DrawRectangleRec(rec, color); if (mouseScaleReady) { diff --git a/src/raylib.h b/src/raylib.h index c6adb2ec8..372633ae8 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1276,6 +1276,7 @@ RLAPI void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, V // Color/pixel related functions RLAPI Color Fade(Color color, float alpha); // Returns color with alpha applied, alpha goes from 0.0f to 1.0f +RLAPI Color FadeBetween(Color from, Color to, float t); // Returns color interpolated between two inputs by t 0.0f to 1.0f RLAPI int ColorToInt(Color color); // Returns hexadecimal value for a Color RLAPI Vector4 ColorNormalize(Color color); // Returns Color normalized as float [0..1] RLAPI Color ColorFromNormalized(Vector4 normalized); // Returns Color from normalized values [0..1] diff --git a/src/textures.c b/src/textures.c index 9e7d1fb70..9f38d7a5a 100644 --- a/src/textures.c +++ b/src/textures.c @@ -3552,6 +3552,22 @@ Color Fade(Color color, float alpha) return (Color){color.r, color.g, color.b, (unsigned char)(255.0f*alpha)}; } +// Returns color interpolated between two inputs by t 0.0f to 1.0f +Color FadeBetween(Color from, Color to, float t) +{ + if (t < 0.0f) t = 0.0f; + if (t > 1.0f) t = 1.0f; + + Color out; + + out.r = (unsigned char) (((1.0f - t) * from.r) + (t * to.r)); + out.g = (unsigned char) (((1.0f - t) * from.g) + (t * to.g)); + out.b = (unsigned char) (((1.0f - t) * from.b) + (t * to.b)); + out.a = (unsigned char) (((1.0f - t) * from.a) + (t * to.a)); + + return out; +} + // Returns hexadecimal value for a Color int ColorToInt(Color color) {