/    Sign up×
Community /Pin to ProfileBookmark

How to create a Java Script for browser-safe colors!!!

Hi is ther anyone can help me to create a java script program to generate and display the browser-safe colors on the browser..
rgds
om…

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJan 05.2007 — You are welcome to this one, it's old (I used it in IE4 and Netscape 4)

but still usable. It makes a table of websafe colors, with titles that

show the hexcode of each cell.

[CODE]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Safe Colors Table</title>
<style type="text/css">
<!--
td{cursor:pointer;cursor:hand}
-->
</style>
</head>
<body>
<h1>Web Safe Colors</h1>

<script type="text/javascript">
<!--

var hexPairs = new Array("00","33","66","99","CC","FF");
var L=hexPairs.length,str = '',temp;
var bstr= (document.layers)?'bgcolor="#' : 'style="background-color:#'

document.writeln('<div><table border="2" ><tbody>');
for (var i = 0; i <L; i++) {
document.writeln('<tr>');
for (var j = 0; j < L; j++) {
for (var k = 0; k < L; k++) {
var temp= ''+hexPairs[i] +''+ hexPairs[j]+'' + hexPairs[k];
str += '<td '+ bstr + temp + '" title="#'+ temp + '">';
str +='__</td>';
}
document.writeln(str);
str = '';
}
document.writeln('</tr>');
}
document.writeln('</tbody></table>');
document.close();

//-->
</script>
<noscript><a href="index.html">OOps...Better get out of here, it's nothing.</a></noscript>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@felgallJan 05.2007 — If you look at both 256 colour mode and 65k colour mode then there are only 7 safe colours and all of them are shades of green. If you look as 65k and 16M colour mode then there are 65k safe colours. The so called safe colours involving the hex pairs that the script above generates is only safe if you disregard 65k mode. As most people have their browser set to either 65k or 16M mode these days the so called safe colours are no longer of any relevance and if you want to include both 256 colour and 65k modes in your consideration then only the 7 shades of green will display the same shade in both.
Copy linkTweet thisAlerts:
@om_26authorJan 08.2007 — Hi mrhoo thx alots for ur codes..well its works but am trying to understand the codes..very confusing !! ..posible for any explanation especially the variable declaration and the for-loop part..!!!!!!!!!!!!!!!!!
Copy linkTweet thisAlerts:
@mrhooJan 08.2007 — This one is simpler, but more interesting.

It returns a random color in rgb format, but if you call randomColor('safe') it returns a 'safe' random color

[CODE]function randomColor(radx){
var str= 'rgb(';
for(var i=0; i<3; i++){
if(radx== 'safe'){
var tem= Math.round(Math.random()*5);
str+= (tem*20)+ '%';
}
else str+= Math.round(Math.random()*255);
str+= (i< 2)? ',': ')';
}
return str;
}[/CODE]


what is being looped through in that table are the hexadecimal equivilents of these decimal percentages in sets of 3- the red, green, and blue components of the complete color string.

For instance, in hexadecimal, 33 has the same ratio to ff that 20 has to 100 in decimal.

Likewise 66 and 40, 99 and 60, CC and 80, and ff and 100.

I don't pay any attention to web safe these days, though we all did ten years ago. But I still like hexadecimal math.
×

Success!

Help @om_26 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...