如何在java中转换Hex颜色?

时间:2012-11-24 20:46:34

标签: android colors hex

我有六角形rgb颜色和黑白面具。它是两个整数数组:

mColors = new int[] {
                 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
                 0xFFFFFF00, 0xFFFF0000
             };
mColorsMask = new int[] {
                     0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF,
                     0xFFFFFFFF, 0xFF000000
                 };

我需要将颜色转换为黑色,具体取决于对比度。对比度是整数值,范围从0到255: enter image description here

使用白色一切都很好,我进行了字节添加:

int newHexColor = (contrast << 16) | (contrast << 8) | contrast | mColors[i];
newColorsArray[i] = mode;

如何将其转换为黑色?

2 个答案:

答案 0 :(得分:0)

您可以使用对比度将图像设为黑色。

参见代码..

public static Bitmap createContrast(Bitmap src, double value) {
    // image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;
    // get contrast value
    double contrast = Math.pow((100 + value) / 100, 2);

    // scan through all pixels
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get pixel color
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            // apply filter contrast for every channel R, G, B
            R = Color.red(pixel);
            R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(R < 0) { R = 0; }
            else if(R > 255) { R = 255; }

            G = Color.red(pixel);
            G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(G < 0) { G = 0; }
            else if(G > 255) { G = 255; }

            B = Color.red(pixel);
            B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(B < 0) { B = 0; }
            else if(B > 255) { B = 255; }

            // set new pixel color to output bitmap
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    return bmOut;
 }

在方法调用上将double值设置为50。例如createContrast(Bitmap src, 50)

答案 1 :(得分:0)

您可以考虑使用HSB色彩空间。它似乎更适合你想要做的事情。特别是,你会看到那些在你的“我想要的”形象中最终变成黑色的角度?这些对应于60,180和300度的“色调”(Java中为1.0 / 6,3.0 / 6和5.0 / 6)。白色对应于0度,120度和240度(Java中为0,1.0 / 3和2.0 / 3) - 并非巧合的是,这些角度的颜色是原色(即三个RGB组件中的两个是零)。

您要做的是找到颜色的色调与最近的原色之间的差异。 (应小于1/6。)将其缩放(乘以6应该这样做),得到0到1.0之间的值。这将给你一个“杂质”值,它基本上是与最近的原色的偏差。当然,从1.0中减去的数字会给你“纯度”,或者与原色的接近程度。

您可以使用相应的值作为R,G和B,以1.0f的alpha值为基础,根据杂质或纯度创建灰度颜色。

public Color getMaskColor(Color c) {
    float[] hsv = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
    float hue = hsv[0];

    // 0, 1/3, and 2/3 are the primary colors.  Find the closest one to c,
    // by rounding c to the nearest third.
    float nearestPrimaryHue = Math.round(hue * 3.0f) / 3.0f;

    // difference between hue and nearestPrimaryHue <= 1/6
    // Multiply by 6 to get a value between 0 and 1.0
    float impurity = Math.abs(hue - nearestPrimaryHue) * 6.0f;
    float purity = 1.0f - impurity;

    // return a greyscale color based on the "purity"
    // (for #FF0000, would return white)
    // using impurity would return black instead
    return new Color(purity, purity, purity, 1.0f);
}

您可以使用返回颜色的颜色分量作为“对比度”值,或者更改函数以便根据需要返回“纯度”或“杂质”。

注意,灰度颜色的数学运算很糟糕。 (Java计算HSB的方式,纯灰色只是红色(色调= 0),没有色调(饱和度= 0)。唯一改变的组件是亮度。)但是因为你的色轮没有灰度颜色......