Skip to content

Commit 39c888b

Browse files
committed
Decompose PremultipliedColour structure into RGBA fields
1 parent 7be757a commit 39c888b

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

osu.Framework/Graphics/Colour/PremultipliedColour.cs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,62 @@
33

44
using System;
55
using osuTK.Graphics;
6+
using SixLabors.ImageSharp.PixelFormats;
67

78
namespace osu.Framework.Graphics.Colour
89
{
910
/// <summary>
10-
/// Represents a <see cref="Color4"/> provided in premultiplied-alpha form.
11+
/// Represents a structure for containing a "premultiplied colour".
1112
/// </summary>
1213
public readonly struct PremultipliedColour : IEquatable<PremultipliedColour>
1314
{
1415
/// <summary>
15-
/// The <see cref="Color4"/> after alpha multiplication.
16+
/// The red component of this colour multiplied by the value of <see cref="Occlusion"/>.
1617
/// </summary>
17-
public readonly Color4 Premultiplied;
18+
public readonly float PremultipliedR;
1819

19-
private PremultipliedColour(Color4 premultiplied)
20-
{
21-
Premultiplied = premultiplied;
22-
}
20+
/// <summary>
21+
/// The green component of this colour multiplied by the value of <see cref="Occlusion"/>.
22+
/// </summary>
23+
public readonly float PremultipliedG;
2324

2425
/// <summary>
25-
/// Creates a <see cref="PremultipliedColour"/> from a straight-alpha colour.
26+
/// The blue component of this colour multiplied by the value of <see cref="Occlusion"/>.
2627
/// </summary>
27-
/// <param name="colour">The straight-alpha colour.</param>
28-
public static PremultipliedColour FromStraight(Color4 colour)
28+
public readonly float PremultipliedB;
29+
30+
/// <summary>
31+
/// The alpha component of this colour, often referred to as "occlusion" instead of "opacity" in the context of premultiplied colours.
32+
/// </summary>
33+
public readonly float Occlusion;
34+
35+
public PremultipliedColour(float premultipliedR, float premultipliedG, float premultipliedB, float occlusion)
2936
{
30-
colour.R *= colour.A;
31-
colour.G *= colour.A;
32-
colour.B *= colour.A;
33-
return new PremultipliedColour(colour);
37+
PremultipliedR = premultipliedR;
38+
PremultipliedG = premultipliedG;
39+
PremultipliedB = premultipliedB;
40+
Occlusion = occlusion;
3441
}
3542

3643
/// <summary>
37-
/// Creates a <see cref="PremultipliedColour"/> from a premultiplied-alpha colour.
44+
/// Creates a <see cref="Rgba32"/> containing the premultiplied components of this colour.
3845
/// </summary>
39-
/// <param name="colour">The premultiplied-alpha colour.</param>
40-
public static PremultipliedColour FromPremultiplied(Color4 colour) => new PremultipliedColour(colour);
46+
public Rgba32 ToPremultipliedRgba32() => new Rgba32(PremultipliedR, PremultipliedG, PremultipliedB, Occlusion);
47+
48+
/// <summary>
49+
/// Creates a <see cref="PremultipliedColour"/> from a straight-alpha colour.
50+
/// </summary>
51+
/// <param name="colour">The straight-alpha colour.</param>
52+
public static PremultipliedColour FromStraight(Color4 colour) => new PremultipliedColour(
53+
colour.R * colour.A,
54+
colour.G * colour.A,
55+
colour.B * colour.A,
56+
colour.A);
4157

42-
public bool Equals(PremultipliedColour other) => Premultiplied.Equals(other.Premultiplied);
58+
public bool Equals(PremultipliedColour other)
59+
=> PremultipliedR == other.PremultipliedR &&
60+
PremultipliedG == other.PremultipliedG &&
61+
PremultipliedB == other.PremultipliedB &&
62+
Occlusion == other.Occlusion;
4363
}
4464
}

osu.Framework/Graphics/OpenGL/GLRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected override void DeleteFrameBufferImplementation(IFrameBuffer frameBuffer
239239
protected override void ClearImplementation(ClearInfo clearInfo)
240240
{
241241
if (!clearInfo.Colour.Equals(CurrentClearInfo.Colour))
242-
GL.ClearColor(clearInfo.Colour.Premultiplied);
242+
GL.ClearColor(new Color4(clearInfo.Colour.PremultipliedR, clearInfo.Colour.PremultipliedG, clearInfo.Colour.PremultipliedB, clearInfo.Colour.Occlusion));
243243

244244
if (clearInfo.Depth != CurrentClearInfo.Depth)
245245
{

osu.Framework/Graphics/Shaders/Types/UniformColour.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Runtime.InteropServices;
55
using osu.Framework.Graphics.Colour;
6-
using osuTK.Graphics;
76

87
namespace osu.Framework.Graphics.Shaders.Types
98
{
@@ -16,16 +15,16 @@ public record struct UniformColour
1615
public UniformVector4 Value;
1716

1817
public static implicit operator PremultipliedColour(UniformColour value)
19-
=> PremultipliedColour.FromPremultiplied(new Color4(value.Value.X, value.Value.Y, value.Value.Z, value.Value.W));
18+
=> new PremultipliedColour(value.Value.X, value.Value.Y, value.Value.Z, value.Value.W);
2019

2120
public static implicit operator UniformColour(PremultipliedColour value) => new UniformColour
2221
{
2322
Value =
2423
{
25-
X = value.Premultiplied.R,
26-
Y = value.Premultiplied.G,
27-
Z = value.Premultiplied.B,
28-
W = value.Premultiplied.A,
24+
X = value.PremultipliedR,
25+
Y = value.PremultipliedG,
26+
Z = value.PremultipliedB,
27+
W = value.Occlusion,
2928
}
3029
};
3130
}

osu.Framework/Graphics/Textures/PremultipliedImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public PremultipliedImage(int width, int height)
2727
}
2828

2929
public PremultipliedImage(int width, int height, PremultipliedColour colour)
30-
: this(new Image<Rgba32>(width, height, new Rgba32(colour.Premultiplied.R, colour.Premultiplied.G, colour.Premultiplied.B, colour.Premultiplied.A)))
30+
: this(new Image<Rgba32>(width, height, colour.ToPremultipliedRgba32()))
3131
{
3232
}
3333

osu.Framework/Graphics/Veldrid/Pipelines/GraphicsPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public override void Begin()
6363
/// <param name="clearInfo">The clearing parameters.</param>
6464
public void Clear(ClearInfo clearInfo)
6565
{
66-
Commands.ClearColorTarget(0, clearInfo.Colour.Premultiplied.ToRgbaFloat());
66+
Commands.ClearColorTarget(0, clearInfo.Colour.ToPremultipliedRgbaFloat());
6767

6868
var framebuffer = currentFrameBuffer?.Framebuffer ?? Device.SwapchainFramebuffer;
6969
if (framebuffer.DepthTarget != null)

osu.Framework/Graphics/Veldrid/VeldridExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
using System.Runtime.InteropServices;
88
using osu.Framework.Extensions.EnumExtensions;
99
using osu.Framework.Extensions.ObjectExtensions;
10+
using osu.Framework.Graphics.Colour;
1011
using osu.Framework.Graphics.Rendering;
1112
using osu.Framework.Graphics.Textures;
1213
using osu.Framework.Logging;
13-
using osuTK.Graphics;
1414
using SharpGen.Runtime;
1515
using Veldrid;
1616
using Veldrid.MetalBindings;
@@ -27,7 +27,8 @@ namespace osu.Framework.Graphics.Veldrid
2727
{
2828
internal static class VeldridExtensions
2929
{
30-
public static RgbaFloat ToRgbaFloat(this Color4 colour) => new RgbaFloat(colour.R, colour.G, colour.B, colour.A);
30+
public static RgbaFloat ToPremultipliedRgbaFloat(this PremultipliedColour colour)
31+
=> new RgbaFloat(colour.PremultipliedR, colour.PremultipliedG, colour.PremultipliedB, colour.Occlusion);
3132

3233
public static BlendFactor ToBlendFactor(this BlendingType type)
3334
{

0 commit comments

Comments
 (0)