This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How to produce html color codes programatically in java?

 
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
 
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
 
Sheriff
Posts: 21865
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Marshal
Posts: 68044
258
  • 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.
 
Marshal
Posts: 67362
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
lowercase baba
Posts: 12797
51
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: 68044
258
  • 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
Posts: 12797
51
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.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!