c#中位图get亮度()算法。

时间:2022-11-17 20:58:24

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.

我想在java中实现一个函数,它计算颜色的亮度和c# get亮度()函数完全一样。所以我需要知道c#中使用的精确算法。这里有一些算法但它们都有5%的误差。

1 个解决方案

#1


3  

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

使用官方来源:http://referencesource.microsoft.com/ System.Drawing / commonui /系统/绘画/ Color.cs # 23 adaaa39209cc1f

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;
}

#1


3  

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

使用官方来源:http://referencesource.microsoft.com/ System.Drawing / commonui /系统/绘画/ Color.cs # 23 adaaa39209cc1f

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;
}