Getting random color using a random class?

My application requirement is a light but not transparent color for list items. I get random colors, but it is very dark. And I don’t know in which range I can only get light colors. The random range is as follows:

Random rndm = new Random(); int color = Color.argb(255, rndm.nextInt(256), rndm.nextInt(256), rndm.nextInt(256)); RelativeLayout rl=(RelativeLayout)rowView.findViewById(R.id.front); rl.setBackgroundColor(color); 
+6
source share
6 answers

You need lighter colors. This means that RGB component values ​​must be between 128 and 255.

Since Random.nextInt(int max) returns int between 0 inclusive and max exclusive , you can try something like this:

 Random rnd = new Random(); int r = rnd.nextInt(128) + 128; // 128 ... 255 int g = rnd.nextInt(128) + 128; // 128 ... 255 int b = rnd.nextInt(128) + 128; // 128 ... 255 Color clr = Color.rgb(r, g, b); 
+5
source

I pointed out to you an idea that I will explain here:

 Random random = new Random(); int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)); Bitmap bitmap = new Bitmap(context); bitmap.eraseColor(color); Palette.from(bitmap).getLightVibrantColor(); 

Cm? I create a Bitmap dummy, fill it with your randomly generated color, and then use the Palette API to get a “bright bright” color.

+4
source

As a rule, to obtain a bright color, one of the RGB values ​​is required that is greater than 128, so you can get 3 random values ​​and check that one of them must be greater than 128. The higher the value, the brighter it will be.

You can check that the average value is greater than 128, it can bring a good result.

 public int getRandomColor() { Random rndm = new Random(); int r = rndm.nextInt(256); int g = rndm.nextInt(256); int b = rndm.nextInt(256); int adjust = 0; if( (r + g + b)/3 < 128) int adjust = (128 - (r + g + b)/3)/3; r += adjust; g += adjust; b += adjust; return Color.argb(255, r, g, b); } 
+4
source

To get a bright random color, you want each channel to have a minimum value.

Here is the code:

 // minimumBrightnessRatio between 0 and 1. 1 = all white, 0 = any possible color. Best results would be 0.5. public int getRandomColor(float minimumBrightnessRatio) { Random rndm = new Random(); final int minimumColorValue = (int)(255f * minimumBrightnessRatio); final int variableColorValue = 255 - minimumColorValue; final int r = minimumColorValue + rndm.nextInt(variableColorValue); final int g = minimumColorValue + rndm.nextInt(variableColorValue); final int b = minimumColorValue + rndm.nextInt(variableColorValue); return Color.argb(255, r, g, b); } 
+3
source

You need to limit the color space you select. Right now you are using the full range of RGB, and of course half the dark colors. Therefore, I suggest you set the base color range for all channels (by experimenting or finding your favorite graphics adapter) and just randomize the range above this baseline.

 final int basec = 128; //baseline would be 50% gray Random rndm = new Random(); int color = Color.argb(255, basec + rndm.nextInt(256-basec), basec + rndm.nextInt(256-basec), basec + rndm.nextInt(256-basec)); RelativeLayout rl=(RelativeLayout)rowView.findViewById(R.id.front); rl.setBackgroundColor(color); 
+2
source

When working with light intensity (in your case you need light colors), the HSI model comes in handy. You can randomly generate intensities between 0.8 and 1 and randomize hue and saturation. Then convert it to RGB.

+2
source

Source: https://habr.com/ru/post/986550/


All Articles