Hex to BGR Hex conversion for KML color in Java

若如初见. 提交于 2019-12-12 02:17:52

问题


I'm building a utility that takes placemarks on a web based map and exports them to KML for use in Google Earth. The problem is the hex values I'm given are standard RGB, whereas KML needs BGR (AABBGGRR where AA is the alpha, but that's not relevant here). For simple colors like red (FF0000), the conversion is simple: 0000FF. However, I've found for something like 7DCCFF (which is like a light blue), simply reversing the string doesn't yield the same color in Google Earth. Am I missing something blatantly obvious here?

Thanks in advance!


回答1:


How about:

Splitting with regex (?<=\G..)

 String color = "123456";
 String[] list = color.split("(?<=\\G..)");
 for(String s : list) {
     System.out.println(s);
 }

Which will give you output of:

12
34
56

Now you simply do some swap among list[0], list[1] and list[2]

The regex (?<=\G..) matches an empty string that has the last match (\G) followed by two characters (..) before it ((?<= ))




回答2:


I've developped a simple PHP tool to convert the KML / RGB colors, using these two PHP functions :

function rgbToKml($color, $aa="ff"){
$rr = substr($color, 0, 2);
$gg = substr($color, 2, 2);
$bb = substr($color, 4, 2);
return $aa.$bb.$gg.$rr;
}
function kmlToRgb($color){
$rr = substr($color, 6, 2);
$gg = substr($color, 4, 2);
$bb = substr($color, 2, 2);
return $rr.$gg.$bb;
}

Check the example here : http://netdelight.be/kml/



来源:https://stackoverflow.com/questions/12534085/hex-to-bgr-hex-conversion-for-kml-color-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!