Colour stuff

here let's make a thread
42 Replies
LukeAbby
LukeAbby4y ago
public Color generateRandomColor(Color mix) {
Random random = new Random();
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);

// mix the color
if (mix != null) {
red = (red + mix.getRed()) / 2;
green = (green + mix.getGreen()) / 2;
blue = (blue + mix.getBlue()) / 2;
}

Color color = new Color(red, green, blue);
return color;
}
public Color generateRandomColor(Color mix) {
Random random = new Random();
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);

// mix the color
if (mix != null) {
red = (red + mix.getRed()) / 2;
green = (green + mix.getGreen()) / 2;
blue = (blue + mix.getBlue()) / 2;
}

Color color = new Color(red, green, blue);
return color;
}
Daniel Thorp
Daniel Thorp4y ago
The part I don't get is getRed, getGreen, getBlue Are those methods of the Color class?
LukeAbby
LukeAbby4y ago
random.nextInt(256) is a 1:1 translation of Math.floor(Math.random() * 256) which makes a number from 0-255. I'm 99% sure that mix.getXYZ() gets the red/green/blue in the form of 0-255. The Color type is irrelevant here. yeah they should be
Daniel Thorp
Daniel Thorp4y ago
function generateRandomColor(mix) {
let red = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);

// mix the color
if (mix != null) {
red = (red + mix[0]) / 2;
green = (green + mix[1]) / 2;
blue = (blue + mix[2]) / 2;
}

let color = [red, green, blue];
return color;
}
function generateRandomColor(mix) {
let red = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);

// mix the color
if (mix != null) {
red = (red + mix[0]) / 2;
green = (green + mix[1]) / 2;
blue = (blue + mix[2]) / 2;
}

let color = [red, green, blue];
return color;
}
LukeAbby
LukeAbby4y ago
Yup I was typing it out and got this:
const sourceColor = [255, 255, 255]; // This is just white and will result in pastel colourations you can mess with this to get better results
function generateRandomColor() {
const red = Math.floor((Math.random() * 256 + sourceColor[0]) / 2)
const green = Math.floor((Math.random() * 256 + sourceColor[1]) / 2)
const blue = Math.floor((Math.random() * 256 + sourceColor[2]) / 2);

return [red, green, blue];
}
const sourceColor = [255, 255, 255]; // This is just white and will result in pastel colourations you can mess with this to get better results
function generateRandomColor() {
const red = Math.floor((Math.random() * 256 + sourceColor[0]) / 2)
const green = Math.floor((Math.random() * 256 + sourceColor[1]) / 2)
const blue = Math.floor((Math.random() * 256 + sourceColor[2]) / 2);

return [red, green, blue];
}
I just merged the conditional with the assignments
Daniel Thorp
Daniel Thorp4y ago
They are the same
LukeAbby
LukeAbby4y ago
yeah they are well not strictly as you have a typo mix.[2] but that's pedantic
Daniel Thorp
Daniel Thorp4y ago
not anymore 😜
LukeAbby
LukeAbby4y ago
oh and you do need to use math.floor twice
red = Math.floor((red + mix[0]) / 2);
green = Math.floor((green + mix[1]) / 2);
blue = Math.floor((blue + mix[2]) / 2);
red = Math.floor((red + mix[0]) / 2);
green = Math.floor((green + mix[1]) / 2);
blue = Math.floor((blue + mix[2]) / 2);
cuz if you get a colour like red = 5 and mix[0] = 2 then (5 + 2) / 2 = 3.5 and you want an integer colour as it turns out Java's division operator on integers automatically floors the result e.g. in JS 5 / 2 = 2.5 but in Java 5 / 2 = 2 while 5.0 / 2.0 = 2.5
LukeAbby
LukeAbby4y ago
is that a satisfying result?
Daniel Thorp
Daniel Thorp4y ago
The colors are a bit muted. I also feel like the top and bottom should be more different
Daniel Thorp
Daniel Thorp4y ago
No description
Daniel Thorp
Daniel Thorp4y ago
No description
Daniel Thorp
Daniel Thorp4y ago
You can't even see a gradient on this one
Want results from more Discord servers?
Add your server