0

I want to generate/randomize a color and then I want second one that is close to the generated one. This is how I generate the colors ftm:

Paint colors = new Paint();

int red = ran.nextInt(256-100)+100;
int green = ran.nextInt(256-100)+100;
int blue = ran.nextInt(256-100)+100;

colors.setARGB(255, red, green, blue);

and later the 2nd color I generate like this:

switch (ran.nextInt(3)) {
        case 0:
            red = red - (40 - level);
            break;
        case 1:
            green = green - (40 - level);
            break;
        default:
            blue = blue - (40-level);
            break;
        }

The problem is that it works in some cases and sometimes it can give me a 2nd color that is off by miles. Is there another, better and easier way to generate these colors?

br

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jedi Schmedi
  • 746
  • 10
  • 36
  • 2
    there's other color spaces, like hsv, which are better suited to manipulation for producing "similar" colors. – Marc B Oct 22 '14 at 19:11
  • @MarcB +1 ...and tinyColor.js easily converts between rgb and hsl: http://bgrins.github.io/TinyColor/docs/tinycolor.html – markE Oct 22 '14 at 19:50

2 Answers2

1

You need to create a real random number between 0 and 3:

 Random ran = new Random();
 int max = 3;
 int min = 0;
 int randomNum = ran.nextInt((max - min) + 1) + min;

switch (randomNum ) {
        case 0:
            red = red - (40 - level);
            break;
        case 1:
            green = green - (40 - level);
            break;
        default:
            blue = blue - (40-level);
            break;
        }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

You could use java.awt.Color.brighter() and Color.darker().

Minesweep
  • 13
  • 4