我想在我的 GWT 客户端使用 Color,
我想要这种颜色
public static Color myColor = new Color( 152, 207, 204) ;
如果我使用这个导入
import java.awt.Color;
在客户端它给我错误:
No source code is available for type java.awt.Color; did you forget to inherit a required module
如何不使用 CSS 在 GWT 客户端中使用 RGB 颜色。
最佳答案
您可以编写一个简单的 RGB 到字符串转换器:
public final class Helper {
public static String RgbToHex(int r, int g, int b){
StringBuilder sb = new StringBuilder();
sb.append('#')
.append(Integer.toHexString(r))
.append(Integer.toHexString(g))
.append(Integer.toHexString(b));
return sb.toString();
}
}
并使用它:
nameField.getElement().getStyle().setBackgroundColor(Helper.RgbToHex(50, 100, 150));
---更新---
控制负值、大于255、0-15值的方式比较复杂。
public static String RgbToHex(int r, int g, int b){
StringBuilder sb = new StringBuilder();
sb.append('#')
.append(intTo2BytesStr(r))
.append(intTo2BytesStr(g))
.append(intTo2BytesStr(b));
return sb.toString();
}
private static String intTo2BytesStr(int i) {
return pad(Integer.toHexString(intTo2Bytes(i)));
}
private static int intTo2Bytes(int i){
return (i < 0) ? 0 : (i > 255) ? 255 : i;
}
private static String pad(String str){
StringBuilder sb = new StringBuilder(str);
if (sb.length()<2){
sb.insert(0, '0');
}
return sb.toString();
}
关于gwt - 在 GWT 中使用颜色类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15403471/