I try to display color values like in the image: COLOR: 195r 30g 110b
when I click on button every time display a random color I use this code for generate colors but doesn't display what I want
Random rnd = new Random();
int color = Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
textView.setText("COLOR: " + color);
Random#nextInt(int)
Returns a pseudorandom, uniformly distributed int value between 0 and the specified value.
Color#rgb(int, int, int)
Returns a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.
Color#rgb(256, 256, 256) - Returns an undefined color because the values are out of range [0..255].
So in your case, you'll need something like this:
Random rnd = new Random();
int red = rnd.nextInt(255);
int green = rnd.nextInt(255);
int blue = rnd.nextInt(255);
int color = Color.rgb(red, green, blue);
textView.setText("COLOR: " + red + "r " + green + "g " + blue + "b");
Or:
Random rnd = new Random();
int color = Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
textView.setText("COLOR: " + ((color >> 16) & 0xFF) + "r " + ((color >> 8) & 0xFF) + "g " + (color & 0xFF) + "b");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.