I have a function and would like to generate only grey colors. How can I do this with the code below? I'm pretty new to javascript! I'd appreciate any help. I'm using Paper.js
function onMouseDown(event) {
path = new Path();
path.fillColor = {
hue: Math.random() * 360,
//saturation: 0,
//brightness: 0.5
};
path.add(event.point);
}
Since the example you provide accepts HSB colours you can simply generate a random brightness value. The saturation should be 0 (fully desaturated) and the hue can also be 0 as it is redundant.
The Brightness value should be a number between 0 an 1. Since you want grey specifically you may want to get a value back that's neither too dark or too light. We can make a little method to return a random number between two values.
function getRandomInRange(min, max) {
return Math.random() * (max - min) + min;
}
So in you code you would use it like this:
function onMouseDown(event) {
path = new Path();
path.fillColor = {
hue: 0,
saturation: 0,
brightness: getRandomInRange(0.2, 0.8)
};
path.add(event.point);
}
Update
Paper.js has a Color constructor specifically for greyscale that would also be appropriate to use here. It takes a number from 0 - 1 (where 0 is black) so the getRandomInRange
will work equally well here:
path.fillColor = new Color(getRandomInRange(0.2, 0.8)); //a random grey