Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,34 @@ private void GetRgbValues(out int r, out int g, out int b)
b = (int)(value & ARGBBlueMask) >> ARGBBlueShift;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void MinMaxRgb(out int min, out int max, int r, int g, int b)
{
if (r > g)
{
max = r;
min = g;
}
else
{
max = g;
min = r;
}
if (b > max)
{
max = b;
}
else if (b < min)
{
min = b;
}
}

public float GetBrightness()
{
GetRgbValues(out int r, out int g, out int b);

int min = Math.Min(Math.Min(r, g), b);
int max = Math.Max(Math.Max(r, g), b);
MinMaxRgb(out int min, out int max, r, g, b);

return (max + min) / (byte.MaxValue * 2f);
}
Expand All @@ -504,8 +526,7 @@ public float GetHue()
if (r == g && g == b)
return 0f;

int min = Math.Min(Math.Min(r, g), b);
int max = Math.Max(Math.Max(r, g), b);
MinMaxRgb(out int min, out int max, r, g, b);

float delta = max - min;
float hue;
Expand All @@ -531,8 +552,7 @@ public float GetSaturation()
if (r == g && g == b)
return 0f;

int min = Math.Min(Math.Min(r, g), b);
int max = Math.Max(Math.Max(r, g), b);
MinMaxRgb(out int min, out int max, r, g, b);

int div = max + min;
if (div > byte.MaxValue)
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Drawing.Primitives/tests/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ private void CheckRed(Color color)
[InlineData(51, 255, 51, 0.6f)]
[InlineData(51, 51, 255, 0.6f)]
[InlineData(51, 51, 51, 0.2f)]
[InlineData(0, 51, 255, 0.5f)]
[InlineData(51, 255, 0, 0.5f)]
[InlineData(0, 255, 51, 0.5f)]
[InlineData(255, 0, 51, 0.5f)]
[InlineData(51, 0, 255, 0.5f)]
[InlineData(255, 51, 0, 0.5f)]
public void GetBrightness(int r, int g, int b, float expected)
{
Assert.Equal(expected, Color.FromArgb(r, g, b).GetBrightness());
Expand Down