开发者

How to convert RGB to BGR?

This is probably easy, but I'm trying to convert from a source which provides colors in RGB strings to an output in BGR strings in Java. I've been busting my brain and time on shifting and Long.decode and Long.toHexString.

Feel free to also throw alpha values in there (RGBA -> ABGR), though I think I can extend the principles.

I开发者_JAVA百科 can assume that the hex is in the form specified in the long and int decode:

0x HexDigits  
0X HexDigits 
# HexDigits


For 24bit colors (8 bits to each of R,G,B):

String rgbSource = getRGBSource(); //your function to get a string version of it
int in = Integer.decode(rgbSource);
int red = (in >> 16) & 0xFF;
int green = (in >> 8) & 0xFF;
int blue = (in >> 0) & 0xFF;
int out = (blue << 16) | (green << 8) | (red << 0);


int abgr = Integer.reverseBytes(rgba);


Including supporting code, with the assumption that it is safe to decide whether alpha needs adding based on the string length (consider "0xFFFFFF".length() for example):

String rgb = getRGB();

//decodes a number of hex formats and sets alpha
int rgba = rgb.length() < 8 ? 
           Long.decode(rgb + "ff").intValue() : 
           Long.decode(rgb       ).intValue();

int abgr = Integer.reverseBytes(rgba);

Here's a one line method:

public static String reverseRGB(String rgba) {
    return String.format("%08X",Integer.reverseBytes(Long.decode(rgba.length() < 8 ? rgba + "ff" : rgba).intValue()));
}


If the input is a 6 character rgb string:

String bgr = rgb.substring(4,6) + rgb.substring(2,4) + rgb.substring(0,2);

If the input is an 8 character rgba string:

String abgr = rgba.substring(6,8) + rgba.substring(4,6) + rgba.substring(2,4) + rgba.substring(0,2);

If the input is an int with 8 bit channels:

String bgr = String.format( "%02X%02X%02X" , rgb & 0x00FF , (rgb>>8) & 0x00FF , (rgb>>16) & 0x00FF );
String abgr = String.format( "%02X%02X%02X%02X" , rgba & 0x00FF , (rgba>>8) & 0x00FF , (rgba>>16) & 0x00FF , (rgba>>24) & 0x00FF );
// or
String bgr = String.format( "%06X" , Integer.reverseBytes( rgb ) >> 8 );
String abgr = String.format( "%08X" , Integer.reverseBytes( rgba ) );


Here is how I got it to work, but I really hope there is a better way because this is awful. Please come up with a cleaner, more efficient way to do this, so I can give you rep.

long number = (rgb.length() < 8 ? Long.decode(rgb+ "ff") : Long.decode(rgb)); //decodes a number of hex formats and sets alpha
String abgrColor = (new StringBuilder())
.append((Long.toHexString((number) & 0xFF).length()==2? Long.toHexString((number) & 0xFF): "0"+Long.toHexString((number) & 0xFF)))
.append((Long.toHexString((number>>8) & 0xFF).length()==2? Long.toHexString((number>>8) & 0xFF): "0"+Long.toHexString((number>>8) & 0xFF)))
.append((Long.toHexString((number>>16) & 0xFF).length()==2? Long.toHexString((number>>16) & 0xFF): "0"+Long.toHexString((number>>16) & 0xFF)))
.append((Long.toHexString((number>>24) & 0xFF).length()==2? Long.toHexString((number>>24) & 0xFF): "0"+Long.toHexString((number>>24) & 0xFF)))
.toString();


This should work.

public static int swapByte1And3(int inValue) {
        int swap = inValue & 0xFF;
        swap = swap << 16 | (inValue >>> 16 & 0xFF);
        return inValue & 0xFF00FF00 | swap;
    }

    public static int convertBRGtoRBG(int inColor) {
        return swapByte1And3(inColor);
    }

    public static int convertABRGtoRBGA(int inColor) {
        int swap = inColor >>> 24;
        inColor = convertBRGtoRBG(inColor) << 8;
        return inColor | swap;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜