Quitter
  • Communauté internationale
    • Langue:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티

Need help generating random colors by HSV (not RGB)

Débutant dans la communauté ,
Sep 05, 2020 Sep 05, 2020

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!

SUJETS
Actions et scripting
1.3K
Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines

correct answers 2 bonnes réponses

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() * 
...
Traduire
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;
...
Traduire
Adobe
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() * (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
Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Débutant dans la communauté ,
Sep 05, 2020 Sep 05, 2020

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. 😉

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
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;
};


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

 

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Débutant dans la communauté ,
Sep 05, 2020 Sep 05, 2020
LA PLUS RÉCENTE

Thanks a lot rob day!

 

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

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines