我想在我的GWT客户端中使用Color,
我想要这种颜色
public static Color myColor = new Color( 152, 207, 204) ;
Run Code Online (Sandbox Code Playgroud)
如果我使用此导入
import java.awt.Color;
Run Code Online (Sandbox Code Playgroud)
在客户端它给我错误:
No source code is available for type java.awt.Color; did you forget to inherit a required module
Run Code Online (Sandbox Code Playgroud)
如何通过不使用CSS在GWT客户端中使用RGB颜色。
您可以编写一个简单的RGB到String转换器:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
nameField.getElement().getStyle().setBackgroundColor(Helper.RgbToHex(50, 100, 150));
Run Code Online (Sandbox Code Playgroud)
-更新-
控制负值,大于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();
}
Run Code Online (Sandbox Code Playgroud)