我试图设置一个任意颜色选择器来获取要传递给 Graphics.fillColor() 的值。
该方法的签名是“动态填充颜色(int color)”,因此颜色似乎不是 RGB/RGBA,而是某个整数。 Color 类为这些值定义了一堆常量,但我希望能够使用用户在我的颜色选择器中选择的任何颜色。
我尝试使用以下过程从 RGB 转换为 Hex:
String r_hex_str = p_rbg_color_lst[0].toRadixString(16); //to hexadecimal
String g_hex_str = p_rbg_color_lst[1].toRadixString(16);
String b_hex_str = p_rbg_color_lst[2].toRadixString(16);
String rgb_hex_str = '0x$r_hex_str$g_hex_str$b_hex_str';
int color_rgb_int = int.parse(rgb_hex_str);
return color_rgb_int;
但是 fillColor() 方法对新的颜色参数没有反应。
为了说明,上述过程将 [100, 145, 185] 作为颜色 RGB 三元组,并输出 6590905 作为整数输出。
StageXL.Color.Red 常量为 4294901760
6590905 和 4294901760 的长度甚至不同,这告诉我我使用的算法是错误的......
有任何想法吗?
谢谢
最佳答案
它似乎使用了额外的两个字节来存储 alpha 值。因此,您需要修改代码以添加:
String a_hex_str = 255.toRadixString(16); // 255 is ff, or fully opaque
String r_hex_str = p_rbg_color_lst[0].toRadixString(16);
String g_hex_str = p_rbg_color_lst[1].toRadixString(16);
String b_hex_str = p_rbg_color_lst[2].toRadixString(16);
String rgb_hex_str = '0x$a_hex_str$r_hex_str$g_hex_str$b_hex_str';
int color_rgb_int = int.parse(rgb_hex_str);
return color_rgb_int;
输入 [100, 145, 185] 为 4284780985
关于colors - StageXL 和颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275294/