You are not logged in.

jordi

Professional

  • "jordi" started this thread

Posts: 583

Location: Barcelona

Occupation: creating ideas & coding them

  • Send private message

1

Friday, July 28th 2017, 1:42pm

Random Colors

Do you need to create random colors ? I do, mostly for my testing purpose code where no images are involved because I want to keep it light.

so here you have a the code for such a function

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<action name="getColor">

	getRandomColor(0x); <!-- for layer -->
	getRandomColor(#); 	<!-- for text -->

</action>

<action name="getRandomColor" type="Javascript" devices="html5"><![CDATA[
		
	var letters = '0123456789ABCDEF';
	var color = args[1];
	for (var i = 0; i < 6; i++) {
		color += letters[Math.floor(Math.random() * 16)];
	}		
	
	krpano.set("randomColor", color);

]]></action>


I hope you enjoy it *cool*
everpano.com step beyond 360

Tuur

Sage

Posts: 3,839

Location: Netherlands

Occupation: Krpano custom coding / Virtual Tours / Photography / Musician / Recording engineer

  • Send private message

2

Friday, July 28th 2017, 11:21pm

*thumbsup*

I had a slightly different approach, but like this one more.
Thanks for sharing!
Tuur *thumbsup*

jordi

Professional

  • "jordi" started this thread

Posts: 583

Location: Barcelona

Occupation: creating ideas & coding them

  • Send private message

3

Monday, July 31st 2017, 10:46am

Yes, as always there are many ways to arrive at the almost same point, here another approach even lighter even simpler

Source code

1
2
mul(randomColor, random,999999);
roundval(randomColor);
everpano.com step beyond 360

4

Tuesday, August 1st 2017, 10:32am

Yes, as always there are many ways to arrive at the almost same point, here another approach even lighter even simpler

Source code

1
2
mul(randomColor, random,999999);
roundval(randomColor);


why 999999?
mul(randomColor, random, 16777215);
(16777215 = 0xffffff)

jordi

Professional

  • "jordi" started this thread

Posts: 583

Location: Barcelona

Occupation: creating ideas & coding them

  • Send private message

5

Tuesday, August 1st 2017, 11:03am

999999 in order to get a value of 6 numbers
everpano.com step beyond 360

6

Tuesday, August 1st 2017, 8:04pm

999999 in order to get a value of 6 numbers


a color is a value of 6 HEXadecimal numbers :)
by using 999999 you get only 6% of all the possible colors
and your colors will be mostly blue or red, but never green

This post has been edited 5 times, last edit by "indexofrefraction" (Aug 1st 2017, 8:45pm)


panomaster

Intermediate

Posts: 297

Location: Kobyłka, Poland

Occupation: Virtual Tours - Spherical Panoramas - Krpano developer

  • Send private message

7

Wednesday, August 2nd 2017, 1:31am

Indexofrefraction is right.

But 16777215 (the bigest decimal number of RGB combination) is DEC number and it should be converted to HEX after random multiplication.

Perhaps the code should look like this:

Source code

1
2
3
4
mul(randomColor, random, 16777215);
roundval(randomColor);
tohex(randomColor,'#',6);
<!-- or tohex(randomColor,'0x',6); -->


Anyway, I like this idea, guys and good job Jordi and Indexofrefraction ;)

More complex usage:

Source code

1
2
3
4
5
6
7
8
9
10
11
getRandomColor(color, '#');
trace(get(color));

getRandomColor(color, '0x');
trace(get(color));

<action name="getRandomColor">
	mul(%1, random, 16777215);
	roundval(%1);
	tohex(%1,%2,6);
</action>

This post has been edited 10 times, last edit by "panomaster" (Aug 2nd 2017, 3:18am)


8

Wednesday, August 2nd 2017, 12:44pm

i dont think you need to reconvert to hex...
this is only the case if you need a string with # for the color (ie. #ff1241)

where you have to give a 0x number it is also ok to give the decimal number.
so for tween() and COLORBLEND() etc using 0x0000ff, 0xff or 255 for blue is the same

if i would need a random color method i'd add 3 random values
for red, green and blue (3 times random * 0xff)
due to the "pseudo" randomization in computers
i think this would provide more even distributed results
than using 1 time random * 0xffffff

jordi

Professional

  • "jordi" started this thread

Posts: 583

Location: Barcelona

Occupation: creating ideas & coding them

  • Send private message

9

Wednesday, August 2nd 2017, 1:34pm

The second method I suggested, was an untested way to show it was possible to make it in different and simpler ways, I think the best method out here is the first one I suggest , even it will only work in html5.
everpano.com step beyond 360

10

Wednesday, August 2nd 2017, 10:12pm

Hi,

here another possibility:

Source code

1
2
3
4
<action name="getRandomColor">
  calc(%1, random * 0xFFFFFF);
  tohex(%1, %2, 6);
</action>

In the calc action it would be possible to hexadecimal values and 0xFFFFFF looks easier to understand than 16777215.
And roundval could be skipped, the tohex action automatically convert the value to an integer.
The bit distribution of one random value should be typical good enough for random colors (although I didn't make some static analysis about that ;-))-

Best regards,
Klaus