• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need help generating random colors by HSV (not RGB)

Community Beginner ,
Sep 05, 2020 Sep 05, 2020

Copy link to clipboard

Copied

Hello guys ,

I'm not sure if what I want is even possible, but here goes! The script below generates random fore and-backgroundcolors based on RGB. Instead, I want to generate colors by HSV.

So this...

 

#target photoshop
var Colour = new SolidColor;
Colour.rgb.red = Math.ceil(255*Math.random());
Colour.rgb.green = Math.ceil(255*Math.random());
Colour.rgb.blue = Math.ceil(255*Math.random());
app.foregroundColor = Colour;
Colour.rgb.red = Math.ceil(255*Math.random());
Colour.rgb.green = Math.ceil(255*Math.random());
Colour.rgb.blue = Math.ceil(255*Math.random());
app.backgroundColor = Colour;

 

...would look like something like this : 

 

#target photoshop
var Colour = new SolidColor;
Colour.hsv.hue = Math.ceil(360*Math.random());
Colour.hsv.saturation = Math.ceil(100*Math.random());
Colour.hsv.value = Math.ceil(100*Math.random());
app.foregroundColor = Colour;
Colour.hsv.hue = Math.ceil(360*Math.random());
Colour.hsv.saturation = Math.ceil(100*Math.random());
Colour.hsv.value = Math.ceil(100*Math.random());
app.backgroundColor = Colour;

 

Even better would be if I could select a range in the hue slider. So I can isolate a specific color.

Any help is greately appreciated!

TOPICS
Actions and scripting

Views

878

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Sep 05, 2020 Sep 05, 2020

Why does it matter how you generate 3 random bytes.  The end result is 3 random hex bytes code. That code is represented differently as RGB values, HSB values,  Lab values, and CMYK  values.  It a random 3 byte value in the range 000000-FFFFFF, ie a random number 0 to 16,777,215.  In 8 Bit color depth the are 16,777,215 color settings. That Photoshop represents 5 ways Hex, RGB, HSB, Lab, CMYK. Set the ranges correctly.

 

 

function getRndInteger(min, max) {
    return Math.floor(Math.random() * 
...

Votes

Translate

Translate
Community Expert , Sep 05, 2020 Sep 05, 2020

Photoshop uses HSB not HSV. Something like this would let you set a min/max range for hue:

 

#target photoshop
app.backgroundColor = randomHSB(90, 240);
app.foregroundColor = randomHSB(0, 45);

/**
* returns a random HSB color 
* @returns a SolidColor 
* 
*/
function randomHSB(hmin, hmax){
    colorobj = new SolidColor()
    colorobj.hsb.hue = getRndInteger(hmin, hmax);
    colorobj.hsb.saturation = getRndInteger(0, 100);
    colorobj.hsb.brightness = getRndInteger(0, 100); 
    return colorobj;
...

Votes

Translate

Translate
Adobe
Community Expert ,
Sep 05, 2020 Sep 05, 2020

Copy link to clipboard

Copied

Why does it matter how you generate 3 random bytes.  The end result is 3 random hex bytes code. That code is represented differently as RGB values, HSB values,  Lab values, and CMYK  values.  It a random 3 byte value in the range 000000-FFFFFF, ie a random number 0 to 16,777,215.  In 8 Bit color depth the are 16,777,215 color settings. That Photoshop represents 5 ways Hex, RGB, HSB, Lab, CMYK. Set the ranges correctly.

 

 

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min) ) + min;
}

 

 

Capture.jpg

 

You may want to change hsv to hsb. After you set the random Foregroubd abd Background you can retrieve their HSB values.

 

saveforeColor = new SolidColor;
saveforeColor.hsb.brightness = app.foregroundColor.hsb.brightness;
saveforeColor.hsb.hue = app.foregroundColor.hsb.hue;
saveforeColor.hsb.saturation = app.foregroundColor.hsb.saturation;

savebackColor = new SolidColor;
savebackColor.hsb.brightness = app.backgroundColor.hsb.brightness;
savebackColor.hsb.hue = app.backgroundColor.hsb.hue;
savebackColor.hsb.saturation = app.backgroundColor.hsb.saturation;
		
alert("Forground Color:"
	+ "\nbrightness " + saveforeColor.hsb.brightness 
	+ "\nhue "  + saveforeColor.hsb.hue 
	+ "\nsaturation  " + saveforeColor.hsb.saturation 
	+ "\n\nBackground Color:"
	+ "\nbrightness " + savebackColor.hsb.brightness 
	+ "\nhue "  + savebackColor.hsb.hue 
	+ "\nsaturation  " + savebackColor.hsb.saturation 
);

 

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 05, 2020 Sep 05, 2020

Copy link to clipboard

Copied

Thanks a lot for your explanation JJMack!

 

I have a shortcut key assigned to generating new colors for when I'm drawing. The RGB script worked well in general, except the colors were usually too vibrant and difficult to tweak in the.jsx file. HSV/B values seemed more logically to me, but I had no idea how to convert it. Hence my reason for asking. 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2020 Sep 05, 2020

Copy link to clipboard

Copied

Photoshop uses HSB not HSV. Something like this would let you set a min/max range for hue:

 

#target photoshop
app.backgroundColor = randomHSB(90, 240);
app.foregroundColor = randomHSB(0, 45);

/**
* returns a random HSB color 
* @returns a SolidColor 
* 
*/
function randomHSB(hmin, hmax){
    colorobj = new SolidColor()
    colorobj.hsb.hue = getRndInteger(hmin, hmax);
    colorobj.hsb.saturation = getRndInteger(0, 100);
    colorobj.hsb.brightness = getRndInteger(0, 100); 
    return colorobj;
};


/**
* @ returns a random number between min and max
* 
*/
function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;
}; 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 05, 2020 Sep 05, 2020

Copy link to clipboard

Copied

LATEST

Thanks a lot rob day!

 

This is exactly what I was looking for and seems to be working as intended! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines