繁体   English   中英

如何将RGB转换为十六进制

[英]How to convert RGB into Hex

public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                final Bitmap bitmap = ((BitmapDrawable) image.getDrawable())
                        .getBitmap();
                int pixel = bitmap.getPixel(x, y);
                redValue = Color.red(pixel);
                blueValue = Color.blue(pixel);
                greenValue = Color.green(pixel); 

                Log.d("***RGB***", "X: "+x+" Y: "+y /*+" Green: "+greenValue*/);

                selected_color.setText(""+redValue+""+blueValue+""+greenValue);
                selected_color.setTextColor(Color.rgb(redValue, greenValue,
                        blueValue));

            }
        });

我有此代码在触摸图像上提供RGB代码..但我想要图像的十六进制代码..如何将十六进制转换为RGB? 我试过下面的代码,但id无效。

int r=redValue, g=greenValue, b=blueValue;
            String hex = String.format("#%02x%02x%02x", r, g, b);
            selected_colour.setTextColor(Color.rgb(r,g,b));

友善建议我如何将RGB转换为十六进制

您不需要它,只需使用字符串:

selected_color.setTextColor(Color.parseColor("#"+redValue+greenValue+blueValue));

编辑:使用此代码从整数获取十六进制:

Integer.toString(value, 16)

一些理论

RGB颜色是红色,绿色和蓝色的组合: (R,G,B)红色,绿色和蓝色各使用8位,其整数值介于0到255之间。

因此,可以生成的颜色数为:

256×256×256 = 16777216 = 100000016

十六进制颜色代码是6位十六进制数(以16为基数):RRGGBB16

左2位数字代表红色。
2个中间数字代表绿色。
右边的2位数字代表蓝色。

RGB到十六进制的转换
将红色,绿色和蓝色的颜色值从十进制转换为十六进制。
连接红色,绿色和蓝色togather的3个十六进制值:RRGGBB。

例子1

将红色(255,0,0)转换为十六进制颜色代码:

R = 25510 = FF16
G = 010 = 0016
B = 010 = 0016

因此,十六进制颜色代码为:

Hex = FF0000

范例#2

将金色(255,215,0)转换为十六进制颜色代码:

R = 25510 = FF16
G = 21510 = D716
B = 010 = 0016

因此,十六进制颜色代码为:

Hex = FFD700

在Android \\ Java中:

import java.awt.Color;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RGBHexInterConverter {
    static String commaSeparatedRGBPattern = "^(\\d{3}),(\\d{3}),(\\d{3})$";
    static final int HEXLENGTH = 8;
    static final String hexaDecimalPattern = "^0x([\\da-fA-F]{1,8})$";

    public static void main(String[] args) {
        /** Some sample RGB and HEX Values for Conversion */
        String RGBForHexConversion = "128,128,255";
        String hexForRGBConversion = "0x0077c8d2";

        /** Convert from RGB to HEX */
        covertRGBToHex(RGBForHexConversion);

        /** Convert from HEX to RGB */
        convertHexToRGB(hexForRGBConversion);

        /**Pass some invalid RGB value for Hex Conversion*/
        covertRGBToHex("3002,4001,5301");

        /**Pass some invalid HEX value for RGB Conversion*/
        convertHexToRGB("5xY077c8d2"); 

    }

    /**
     * @param hexForRGBConversion
     *            - hex value string for conveting to RGB format. Valid format
     *            is: 0xXXXXXXXX e.g. 0x0077c8d2
     * @return comma separated rgb values in the format rrr,ggg, bbb e.g.
     *         "119,200,210"
     */
    private static String convertHexToRGB(String hexForRGBConversion) {
        System.out.println("...converting Hex to RGB");
        String rgbValue = "";
        Pattern hexPattern = Pattern.compile(hexaDecimalPattern);
        Matcher hexMatcher = hexPattern.matcher(hexForRGBConversion);

        if (hexMatcher.find()) {
            int hexInt = Integer.valueOf(hexForRGBConversion.substring(2), 16)
                    .intValue();

            int r = (hexInt & 0xFF0000) >> 16;
            int g = (hexInt & 0xFF00) >> 8;
            int b = (hexInt & 0xFF);

            rgbValue = r + "," + g + "," + b;
            System.out.println("Hex Value: " + hexForRGBConversion
                    + "\nEquivalent RGB Value: " + rgbValue);
        } else {
            System.out.println("Not a valid Hex String: " + hexForRGBConversion
                    + "\n>>>Please check your input string.");
        }
        System.out.println();
        return rgbValue;

    }

    /**
     * @param rgbForHexConversion
     *           - comma separated rgb values in the format rrr,ggg, bbb e.g.
     *            "119,200,210"
     * @return equivalent hex in the format 0xXXXXXXXX e.g. 0x0077c8d2
     *
     *        If the converted hex value is not 8 characters long, pads the
     *         zeros in the front.
     */
    private static String covertRGBToHex(String rgbForHexConversion) {
        System.out.println("...converting RGB to Hex");
        String hexValue = "";
        Pattern rgbPattern = Pattern.compile(commaSeparatedRGBPattern);
        Matcher rgbMatcher = rgbPattern.matcher(rgbForHexConversion);

        int red;
        int green;
        int blue;
        if (rgbMatcher.find()) {
            red = Integer.parseInt(rgbMatcher.group(1));
            green = Integer.parseInt(rgbMatcher.group(2));
            blue = Integer.parseInt(rgbMatcher.group(3));
            Color color = new Color(red, green, blue);
            hexValue = Integer.toHexString(color.getRGB() & 0x00ffffff);
            int numberOfZeroesNeededForPadding = HEXLENGTH - hexValue.length();
            String zeroPads = "";
            for (int i = 0; i < numberOfZeroesNeededForPadding; i++) {
                zeroPads += "0";
            }
            hexValue = "0x" + zeroPads + hexValue;
            System.out.println("RGB value: " + rgbForHexConversion
                    + "\nEquivalent Hex Value: " + hexValue);
        } else {
            System.out.println("Not a valid RGB String: "+rgbForHexConversion
                    + "\n>>>Please check your inut string.");
        }


        System.out.println();
        return hexValue;
    }
}

暂无
暂无

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

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