Skip to content

Commit 5e44702

Browse files
committed
feat: smooth hue-cycling gradient in AlphaBlendingDemo
Replace the direction-rolling animation (snapping between Horizontal / DiagonalDown / Vertical / DiagonalUp every 1.5 s) with a continuous hue cycle. Three gradient stops are kept 120° apart on the colour wheel and rotate together at ~0.08 hue/s (full cycle ≈ 12 s), producing a smooth flowing colour wash on a fixed DiagonalDown direction.
1 parent 4b53e0e commit 5e44702

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

Examples/DemoApp/DemoWindows/AlphaBlendingDemoWindow.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,40 +197,52 @@ static List<string> MakeBlendPreview(int alpha)
197197
})
198198
.WithAsyncWindowThread(async (win, ct) =>
199199
{
200-
var directions = new[]
201-
{
202-
GradientDirection.Horizontal,
203-
GradientDirection.DiagonalDown,
204-
GradientDirection.Vertical,
205-
GradientDirection.DiagonalUp,
206-
};
207-
int dirIndex = 0;
208-
var lastDirChange = DateTime.Now;
209200
var startTime = DateTime.Now;
210201

202+
// Convert a hue (0.0–1.0) to a fully-saturated RGB color.
203+
static Color HueToColor(double h)
204+
{
205+
h = ((h % 1.0) + 1.0) % 1.0; // normalise to [0,1)
206+
double s = h * 6.0;
207+
int i = (int)s;
208+
double f = s - i;
209+
double q = 1.0 - f;
210+
return i switch
211+
{
212+
0 => new Color(255, (byte)(f * 255), 0),
213+
1 => new Color((byte)(q * 255), 255, 0),
214+
2 => new Color(0, 255, (byte)(f * 255)),
215+
3 => new Color(0, (byte)(q * 255), 255),
216+
4 => new Color((byte)(f * 255), 0, 255),
217+
_ => new Color(255, 0, (byte)(q * 255)),
218+
};
219+
}
220+
211221
while (!ct.IsCancellationRequested)
212222
{
213223
await Task.Delay(50, ct);
214224

225+
double t = (DateTime.Now - startTime).TotalSeconds;
226+
215227
// Pulse panel
216228
var pulse = win.FindControl<ScrollablePanelControl>("pulsePanel");
217229
if (pulse != null)
218230
{
219-
double t = (DateTime.Now - startTime).TotalSeconds;
220231
byte a = (byte)((Math.Sin(t * Math.PI) + 1.0) / 2.0 * 255);
221232
pulse.BackgroundColor = new Color(255, 50, 100, a);
222233
}
223234

224-
// Background animation
235+
// Smooth gradient cycle — three hues spaced 120° apart, rotating over time
225236
var toggle = win.FindControl<CheckboxControl>("animToggle");
226-
if (toggle?.Checked == true &&
227-
(DateTime.Now - lastDirChange).TotalSeconds >= 1.5)
237+
if (toggle?.Checked == true)
228238
{
229-
dirIndex = (dirIndex + 1) % directions.Length;
239+
double phase = t * 0.08; // full hue cycle every ~12 s
240+
var c1 = HueToColor(phase);
241+
var c2 = HueToColor(phase + 1.0 / 3.0);
242+
var c3 = HueToColor(phase + 2.0 / 3.0);
230243
win.BackgroundGradient = new GradientBackground(
231-
ColorGradient.FromColors(Color.Blue, Color.MediumPurple, Color.Orange1),
232-
directions[dirIndex]);
233-
lastDirChange = DateTime.Now;
244+
ColorGradient.FromColors(c1, c2, c3),
245+
GradientDirection.DiagonalDown);
234246
}
235247
}
236248
})

0 commit comments

Comments
 (0)