3

I want to implement a function in java that calculate brightness of a color exactly as same as C# getbrightness() function. So I need to know the exact algorithm that used in C#. There is some algorithms here but all of them have about 5% error.

Community
  • 1
  • 1
Pooya Zafar
  • 45
  • 1
  • 4
  • Luminance != brightness – AK_ Apr 22 '14 at 09:33
  • 1
    Another thing, in Image Processing the term brightness can refer to about 73 similar but different things... – AK_ Apr 22 '14 at 09:36
  • Note that `Color.GetBrightness()` brings back `0.5` for `Blue` as well as `Yellow`. So it really is totally useless for anything in the real world of colors.. - It may have some reason deep inside of GDI+, although I don't what that could be.. – TaW Dec 10 '14 at 16:56

1 Answers1

5

Use official source: http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Color.cs#23adaaa39209cc1f

public float GetBrightness()
{
    float r = (float)R / 255.0f;
    float g = (float)G / 255.0f;
    float b = (float)B / 255.0f;

    float max, min;

    max = r; min = r;

    if (g > max) max = g;
    if (b > max) max = b;

    if (g < min) min = g;
    if (b < min) min = b;

    return (max + min) / 2;
}
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31