• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to produce html color codes programatically in java?  RSS feed

 
Palak Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

What i want, is to write a code that will be capable of generating random html color codes, is there a way to do so?

Thanks
Palak
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there is. Do you know how a html-color-code is build up?
 
Palak Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

no i don't please help me with this.

Thanks
Palak
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two ways of specifying an HTML colour - by name for the known ones (like "red", "green", "blue") or by a "#" followed by the colour's RGB HEX code. Although the specification doesn't require the HEX code to be 6 characters, you really should use 6 characters - the first two for red, the second two for green, the last two for blue. For instance, red then becomes "#ff0000". You can use String.format to quickly achieve this formatting.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you find when you searched for it?
 
Palak Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i got this piece of code to generate random color codes



 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great but that isn't Java. I'm going to assume it is javascript.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case I shall move it to the JavaScript forum.
You do realise that using round and 15 will not give you a random selection? You will either omit F altogether, or F and 0 will have (approximately) half the probability of being used which the other 14 numbers have.
 
Palak Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i want to apply this logic in java.
My requirement is to generate the dark color codes randomly and i am not able to get the exact logic, so please help.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Than convert it to java. That shouldn't be a big problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved back to Beginning Java as that's the target language for this issue.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Palak Agarwal wrote:My requirement is to generate the dark color codes randomly and i am not able to get the exact logic, so please help.

well, first you have to define what you mean by "dark colors".

if you can't get the exact logic, what do you have for the approximate logic? We require that you ShowSomeEffort here, not just demand someone provide it for you.
 
Palak Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I have written the following code in java:




When i run this code couple of times i got two color codes which are entirely different from each other but their color are exactly same. Following are the two produced codes :
#A11C10 and #aa020a

Please if any one has idea how to produce codes where color as well as code both are unique, then reply.
I have to generate around 300 unique codes . Please help.

Thanks
Palak
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not possible to create unique codes by random means, but there are 1.7 million different possibilities, so the changes of duplicates are slight.
You are not using random codes, but there is a bias; by using 15 you may never have an F in your codes. Read what Math.random() returns. you should not use round if you are casting to an int; it is completely unnecessary.
You might do well to insert your codes into a Set, and keep going until the Set contains 300 elements.

Those two colours are different. The difference is slight, but I can see it clearly. This is a11c10 and this is aa020a. See, quite different.
As you have already been told, you will have to work out what you mean by dark colours.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Palak Agarwal,

I have added code tags to your post. Just like making something bold or italic, once you post your source code, highlight it all and click the 'code' button above. It will preserver your formatting and to context sensitive highlighting, making it MUCH easier to read.

You should realize that with 16 million possible colors, many of them will be virtually indistinguishable. The two colors you posted have almost same red (aa - a1 is a difference of 10, almost the same green (1c - 02 is 1A or 26), and almost the same blue (10 - 0a is 6). Your eye may not be able to distinguish them, but the ARE different.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!