From d29b51dfd99eaec28bdc86486e21b6fc1a8ae2f9 Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Mon, 9 Dec 2024 09:06:15 +0100 Subject: [PATCH] Core: Lerp(Color) --- reference/LinearInterpolation/LinearInterpolation.cpp | 6 +++++- uppsrc/Core/Color.cpp | 8 ++++++++ uppsrc/Core/Color.h | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/reference/LinearInterpolation/LinearInterpolation.cpp b/reference/LinearInterpolation/LinearInterpolation.cpp index f0c60bf4b..31788534f 100644 --- a/reference/LinearInterpolation/LinearInterpolation.cpp +++ b/reference/LinearInterpolation/LinearInterpolation.cpp @@ -6,13 +6,17 @@ using namespace Upp; CONSOLE_APP_MAIN { - StdLogSetup(LOG_COUT); + StdLogSetup(LOG_COUT|LOG_FILE); DUMP(Lerp(10, 20, 0.5)); DUMP(Lerp(10.0, 20.0, 0.25)); DUMP(Lerp(Point(10, 20), Point(30, 40), 0.5)); DUMP(Lerp(Size(100, 200), Size(300, 400), 0.5)); DUMP(Lerp(Rect(0, 0, 100, 100), Rect(100, 100, 200, 200), 0.5)); + + DUMP(Lerp(Color(100, 200, 150), Color(200, 100, 120), 0.5)); + DUMP(Lerp(Color(100, 200, 150), Color(200, 100, 120), 1.5)); + DUMP(Lerp(Color(100, 200, 150), Color(200, 100, 120), -1)); } diff --git a/uppsrc/Core/Color.cpp b/uppsrc/Core/Color.cpp index 7e8bb1737..428c914c7 100644 --- a/uppsrc/Core/Color.cpp +++ b/uppsrc/Core/Color.cpp @@ -307,6 +307,14 @@ Color Blend(Color c1, Color c2, int alpha) min(((a * (c2.GetB() - c1.GetB())) >> 8) + c1.GetB(), 255)); } +Color Lerp(Color a, Color b, double t) +{ + auto Ch = [&](byte a, byte b) { + return (byte)clamp(Lerp((double)a, (double)b, t), 0., 255.); + }; + return Color(Ch(a.GetR(), b.GetR()), Ch(a.GetG(), b.GetG()), Ch(a.GetB(), b.GetB())); +} + INITBLOCK { Value::SvoRegister("Color"); } diff --git a/uppsrc/Core/Color.h b/uppsrc/Core/Color.h index ea7cc2f4e..17b56ce54 100644 --- a/uppsrc/Core/Color.h +++ b/uppsrc/Core/Color.h @@ -166,6 +166,8 @@ double ContrastRatio(Color c1, Color c2); Color Blend(Color c1, Color c2, int alpha = 128); +Color Lerp(Color a, Color b, double t); + String ColorToHtml(Color color); Color ColorFromText(const char *s);