Simple function FadeBetween to interpolate between two colours. Small demo in existing example.

This commit is contained in:
Simon 2021-04-02 12:53:33 +01:00
parent b54d96205f
commit 2c969641a6
3 changed files with 29 additions and 1 deletions

View File

@ -12,6 +12,7 @@
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include <math.h>
#define MOUSE_SCALE_MARK_SIZE 12 #define MOUSE_SCALE_MARK_SIZE 12
@ -31,6 +32,11 @@ int main(void)
bool mouseScaleReady = false; bool mouseScaleReady = false;
bool mouseScaleMode = 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 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -39,6 +45,8 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
double time = GetTime();
mousePosition = GetMousePosition(); mousePosition = GetMousePosition();
if (CheckCollisionPointRec(mousePosition, rec) && 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.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = 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; 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); DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);
DrawRectangleRec(rec, Fade(GREEN, 0.5f)); DrawRectangleRec(rec, color);
if (mouseScaleReady) if (mouseScaleReady)
{ {

View File

@ -1276,6 +1276,7 @@ RLAPI void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, V
// Color/pixel related functions // 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 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 int ColorToInt(Color color); // Returns hexadecimal value for a Color
RLAPI Vector4 ColorNormalize(Color color); // Returns Color normalized as float [0..1] RLAPI Vector4 ColorNormalize(Color color); // Returns Color normalized as float [0..1]
RLAPI Color ColorFromNormalized(Vector4 normalized); // Returns Color from normalized values [0..1] RLAPI Color ColorFromNormalized(Vector4 normalized); // Returns Color from normalized values [0..1]

View File

@ -3552,6 +3552,22 @@ Color Fade(Color color, float alpha)
return (Color){color.r, color.g, color.b, (unsigned char)(255.0f*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 // Returns hexadecimal value for a Color
int ColorToInt(Color color) int ColorToInt(Color color)
{ {