javashixi16进制转RGB
时间: 2023-11-09 19:04:36 浏览: 64
以下是Java代码实现将16进制颜色值转换为RGB颜色值:
```java
public static int[] hexToRgb(String hex) {
// 去除 # 号
if (hex.startsWith("#")) {
hex = hex.substring(1);
}
// 分离 R、G、B 三个值
int red = Integer.parseInt(hex.substring(0, 2), 16);
int green = Integer.parseInt(hex.substring(2, 4), 16);
int blue = Integer.parseInt(hex.substring(4, 6), 16);
return new int[]{red, green, blue};
}
```
使用示例:
```java
String hexColor = "#FFA500";
int[] rgbColor = hexToRgb(hexColor);
System.out.println(Arrays.toString(rgbColor)); // [255, 165, 0]
```
其中,`hexColor` 表示待转换的16进制颜色值,`rgbColor` 为转换后的RGB颜色值数组,例如 `[255, 165, 0]` 表示红色分量为 255,绿色分量为 165,蓝色分量为 0。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)