package com.fusionchart.model;
import java.util.Random;
public class RandomColor {
public static void main(String[] args)
{
//红色
String red;
//绿色
String green;
//蓝色
String blue;
//生成随机对象
Random random = new Random();
//生成红色颜色代码
red = Integer.toHexString(random.nextInt(256)).toUpperCase();
//生成绿色颜色代码
green = Integer.toHexString(random.nextInt(256)).toUpperCase();
//生成蓝色颜色代码
blue = Integer.toHexString(random.nextInt(256)).toUpperCase();
//判断红色代码的位数
red = red.length()==1 ? "0" + red : red ;
//判断绿色代码的位数
green = green.length()==1 ? "0" + green : green ;
//判断蓝色代码的位数
blue = blue.length()==1 ? "0" + blue : blue ;
//生成十六进制颜色值
String color = "#"+red+green+blue;
System.out.println(color);
}
}
Java代码生成随机十六进制颜色代码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 之前自己在做瀑布流照片墙时,苦于强迫症,找了好久也没找到心仪的图片,决定用随机的颜色+随机的高度来模拟不同的图片。...
- 将16进制的颜色值转换成UIColor对象@para:hexString: 生成颜色对象的16进制字符串alpha...
- UIColor+CJ a easy way to use UIColor by color name, RGB, ...