Color similarity/distance in RGBA color space

前端 未结 5 937
遇见更好的自我
遇见更好的自我 2020-12-23 14:22

How to compute similarity between two colors in RGBA color space? (where the background color is unknown of course)

I need to remap an RGBA image to

5条回答
  •  不知归路
    2020-12-23 15:05

    Several principles:

    1. When two colors have same alpha, rgbaDistance = rgbDistance * ( alpha / 255). Compatible with RGB color distance algorithm when both alpha are 255.
    2. All Colors with very low alpha are similar.
    3. The rgbaDistance between two colors with same RGB is linearly dependent on delta Alpha.
    double DistanceSquared(Color a, Color b)
    {
        int deltaR = a.R - b.R;
        int deltaG = a.G - b.G;
        int deltaB = a.B - b.B;
        int deltaAlpha = a.A - b.A;
        double rgbDistanceSquared = (deltaR * deltaR + deltaG * deltaG + deltaB * deltaB) / 3.0;
        return deltaAlpha * deltaAlpha / 2.0 + rgbDistanceSquared * a.A * b.A / (255 * 255);
    }
    

提交回复
热议问题