1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Merge branch 'development'

This commit is contained in:
Robert 2022-05-12 23:07:01 +02:00
commit 1c2fe904b7

View File

@ -54,9 +54,8 @@ namespace Artemis.Core
} }
else else
{ {
List<SKColor> colors = this.Select(c => c.Color).ToList();
for (int i = 0; i <= timesToRepeat; i++) for (int i = 0; i <= timesToRepeat; i++)
result.AddRange(colors); result.AddRange(this.Select(c => c.Color));
} }
if (seamless && !IsSeamless()) if (seamless && !IsSeamless())
@ -68,9 +67,7 @@ namespace Artemis.Core
/// <summary> /// <summary>
/// Gets all the positions in the color gradient /// Gets all the positions in the color gradient
/// </summary> /// </summary>
/// <param name="timesToRepeat"> /// <param name="timesToRepeat">The amount of times to repeat the positions</param>
/// The amount of times to repeat the positions
/// </param>
/// <param name="seamless"> /// <param name="seamless">
/// A boolean indicating whether to make the gradient seamless by adding the first color behind the /// A boolean indicating whether to make the gradient seamless by adding the first color behind the
/// last color /// last color
@ -113,19 +110,30 @@ namespace Artemis.Core
/// Gets a color at any position between 0.0 and 1.0 using interpolation /// Gets a color at any position between 0.0 and 1.0 using interpolation
/// </summary> /// </summary>
/// <param name="position">A position between 0.0 and 1.0</param> /// <param name="position">A position between 0.0 and 1.0</param>
public SKColor GetColor(float position) /// <param name="timesToRepeat">The amount of times to repeat the positions</param>
/// <param name="seamless">
/// A boolean indicating whether to make the gradient seamless by adding the first color behind the
/// last color
/// </param>
public SKColor GetColor(float position, int timesToRepeat = 0, bool seamless = false)
{ {
if (!this.Any()) if (!this.Any())
return SKColor.Empty; return new SKColor(255, 255, 255);
ColorGradientStop[] stops = this.ToArray(); SKColor[] colors = GetColorsArray(timesToRepeat, seamless);
if (position <= 0) return stops[0].Color; float[] stops = GetPositionsArray(timesToRepeat, seamless);
if (position >= 1) return stops[^1].Color;
ColorGradientStop left = stops[0]; // If at or over the edges, return the corresponding edge
ColorGradientStop? right = null; if (position <= 0) return colors[0];
foreach (ColorGradientStop stop in stops) if (position >= 1) return colors[^1];
// Walk through the stops until we find the one at or after the requested position, that becomes the right stop
// The left stop is the previous stop before the right one was found.
float left = stops[0];
float? right = null;
foreach (float stop in stops)
{ {
if (stop.Position >= position) if (stop >= position)
{ {
right = stop; right = stop;
break; break;
@ -134,14 +142,22 @@ namespace Artemis.Core
left = stop; left = stop;
} }
if (right == null || left == right) // Get the left stop's color
return left.Color; SKColor leftColor = colors[Array.IndexOf(stops, left)];
position = (float) Math.Round((position - left.Position) / (right.Position - left.Position), 2); // If no right stop was found or the left and right stops are on the same spot, return the left stop's color
byte a = (byte) ((right.Color.Alpha - left.Color.Alpha) * position + left.Color.Alpha); if (right == null || left == right)
byte r = (byte) ((right.Color.Red - left.Color.Red) * position + left.Color.Red); return leftColor;
byte g = (byte) ((right.Color.Green - left.Color.Green) * position + left.Color.Green);
byte b = (byte) ((right.Color.Blue - left.Color.Blue) * position + left.Color.Blue); // Get the right stop's color
SKColor rightColor = colors[Array.IndexOf(stops, right)];
// Interpolate the position between the left and right color
position = MathF.Round((position - left) / (right.Value - left), 2);
byte a = (byte) ((rightColor.Alpha - leftColor.Alpha) * position + leftColor.Alpha);
byte r = (byte) ((rightColor.Red - leftColor.Red) * position + leftColor.Red);
byte g = (byte) ((rightColor.Green - leftColor.Green) * position + leftColor.Green);
byte b = (byte) ((rightColor.Blue - leftColor.Blue) * position + leftColor.Blue);
return new SKColor(r, g, b, a); return new SKColor(r, g, b, a);
} }