1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:07:52 +08:00

Add LegacyUtils class with non linear colour interpolation method

This commit is contained in:
TheOmyNomy 2021-08-09 18:56:47 +10:00
parent 3686c657d2
commit 24accdcab0
2 changed files with 69 additions and 8 deletions

View File

@ -3,9 +3,9 @@
using System;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Utils;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Skinning.Default;
using osu.Game.Utils;
using osuTK.Graphics;
namespace osu.Game.Rulesets.Osu.Skinning.Legacy
@ -37,14 +37,10 @@ namespace osu.Game.Rulesets.Osu.Skinning.Legacy
position -= realBorderPortion;
// Stable interpolates slider body colour directly in sRGB space, and because
// Interpolation.ValueAt() uses linear space, we have to counteract applying it
// by calling ToSRGB() on the input colours, and ToLinear() on the resulting colour.
Color4 outerColour = AccentColour.Darken(0.1f);
Color4 innerColour = lighten(AccentColour, 0.5f);
Color4 outerColour = AccentColour.Darken(0.1f).ToSRGB();
Color4 innerColour = lighten(AccentColour, 0.5f).ToSRGB();
return Interpolation.ValueAt(position / realGradientPortion, outerColour, innerColour, 0, 1).ToLinear();
return LegacyUtils.InterpolateNonLinear(position / realGradientPortion, outerColour, innerColour, 0, 1);
}
/// <summary>

View File

@ -0,0 +1,65 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Transforms;
using osuTK.Graphics;
namespace osu.Game.Utils
{
public static class LegacyUtils
{
public static Color4 InterpolateNonLinear(double time, Color4 startColour, Color4 endColour, double startTime, double endTime, Easing easing = Easing.None)
=> InterpolateNonLinear(time, startColour, endColour, startTime, endTime, new DefaultEasingFunction(easing));
public static Colour4 InterpolateNonLinear(double time, Colour4 startColour, Colour4 endColour, double startTime, double endTime, Easing easing = Easing.None)
=> InterpolateNonLinear(time, startColour, endColour, startTime, endTime, new DefaultEasingFunction(easing));
/// <summary>
/// Interpolates between two sRGB <see cref="Color4"/>s directly in sRGB space.
/// </summary>
public static Color4 InterpolateNonLinear<TEasing>(double time, Color4 startColour, Color4 endColour, double startTime, double endTime, TEasing easing) where TEasing : IEasingFunction
{
if (startColour == endColour)
return startColour;
double current = time - startTime;
double duration = endTime - startTime;
if (duration == 0 || current == 0)
return startColour;
float t = Math.Max(0, Math.Min(1, (float)easing.ApplyEasing(current / duration)));
return new Color4(
startColour.R + t * (endColour.R - startColour.R),
startColour.G + t * (endColour.G - startColour.G),
startColour.B + t * (endColour.B - startColour.B),
startColour.A + t * (endColour.A - startColour.A));
}
/// <summary>
/// Interpolates between two sRGB <see cref="Colour4"/>s directly in sRGB space.
/// </summary>
public static Colour4 InterpolateNonLinear<TEasing>(double time, Colour4 startColour, Colour4 endColour, double startTime, double endTime, in TEasing easing) where TEasing : IEasingFunction
{
if (startColour == endColour)
return startColour;
double current = time - startTime;
double duration = endTime - startTime;
if (duration == 0 || current == 0)
return startColour;
float t = Math.Max(0, Math.Min(1, (float)easing.ApplyEasing(current / duration)));
return new Colour4(
startColour.R + t * (endColour.R - startColour.R),
startColour.G + t * (endColour.G - startColour.G),
startColour.B + t * (endColour.B - startColour.B),
startColour.A + t * (endColour.A - startColour.A));
}
}
}