import java.util.*;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ColorHelper {
public static String RGB2Hex(String rgb){
Matcher matcher = Pattern.compile("(?<=[\\(|\\[])[^\\)|\\]]+").matcher(rgb);
String[] rgbArr = null;
String hex = "";
if(matcher.find()){
String tmp = matcher.group();
rgbArr = tmp.split(",");
}
hex = String.format("#%02X%02X%02X", Integer.parseInt(rgbArr[0].trim()), Integer.parseInt(rgbArr[1].trim()), Integer.parseInt(rgbArr[2].trim()));
return hex;
}
public static List<String> RGBArray2HexArray(String rgb){
Matcher matcher = Pattern.compile("(?<=[\\(|\\[])[^\\)|\\]]+").matcher(rgb);
List<String> rgbArr = new LinkedList<>();
while(matcher.find()){
String[] tmp = matcher.group().split(",");
String hex = String.format("#%02X%02X%02X", Integer.parseInt(tmp[0].trim()), Integer.parseInt(tmp[1].trim()), Integer.parseInt(tmp[2].trim()));
rgbArr.add(hex);
}
return rgbArr;
}
public static void main(String[] args) {
// String co = "rgb(0.8274, 0.051, 0.2078)";
// System.out.println(RGB2Hex(co));
String co2 = "[255, 255, 0]";
System.out.println(RGB2Hex(co2));
// String co3 = "colors = {[51,204,255]/255;\n" +
// "[255,255,0]/255;\n" +
// "[51,204,102]/255;\n" +
// "[51,255,204]/255;\n" +
// "[255,255,153]/255;\n" +
// "[219,186,119]/255;\n" +
// "\n" +
// "[204,255,255]/255;\n" +
// "[102,255,51]/255;\n" +
// "[255,204,0]/255;\n" +
// "[102,153,255]/255;\n" +
// "[153,204,51]/255;};";
// System.out.println(RGBArray2HexArray(co3));
}
}
免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。