繁体   English   中英

如何在Java中转换十六进制颜色?

[英]How to convert Hex color in java?

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

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

我需要根据对比度将颜色转换为黑色值。 对比度是介于0到255之间的整数值: 在此处输入图片说明

白色一切都好,我做字节加法:

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

如何将其转换为黑色?

您可以使用对比度使图像黑白色。

参见代码。

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)

您可能会考虑使用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)来基于杂质或纯度创建灰度颜色。

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)。唯一改变的成分是亮度。)但是由于色轮没有灰度颜色,所以...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM