Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RGB Min Max evaluation performance by using 2 or 3 comparison… #50622

Merged
merged 5 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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,49 @@ private void GetRgbValues(out int r, out int g, out int b)
b = (int)(value & ARGBBlueMask) >> ARGBBlueShift;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simplify this to use ? assignment?

Suggested change
if (r < g)
max = g;
else
max = r;
max = r < g ? g : r;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.

}
else
{
min = r;
max = g;
}
}
else
{
if (r < b)
{
max = b;
if (g < r)
min = g;
else
min = r;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto:

Suggested change
if (g < r)
min = g;
else
min = r;
min = g < r ? g : r;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.

}
else
{
max = r;
min = g;
}
}
}

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);
int min, max;
MinMaxRgb(out min, out max, r, g, b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int min, max;
MinMaxRgb(out min, out max, r, g, b);
MinMaxRgb(out int min, out int max, r, g, b);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.


return (max + min) / (byte.MaxValue * 2f);
}
Expand All @@ -504,8 +541,8 @@ 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);
int min, max;
MinMaxRgb(out min, out max, r, g, b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int min, max;
MinMaxRgb(out min, out max, r, g, b);
MinMaxRgb(out int min, out int max, r, g, b);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.


float delta = max - min;
float hue;
Expand All @@ -531,8 +568,8 @@ 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);
int min, max;
MinMaxRgb(out min, out max, r, g, b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int min, max;
MinMaxRgb(out min, out max, r, g, b);
MinMaxRgb(out int min, out int max, r, g, b);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.


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