/    Sign up×
Community /Pin to ProfileBookmark

Color Formats (from RGB to Hex)

I don’t know where to put this topic
It is about Color formats

Str8 to the point
I want to make a script to make a code for text like this :
[color=#F11C00]A[/color][color=#E33800]B[/color][color=#D55400]C[/color][color=#C77000]D[/color][color=#B98C00]E[/color][color=#9DC400]F[/color][color=#8FE000]G[/color][color=#81FC00]H[/color]
I could make it ! ?

But the colors will be in RGB format
How to convert it to Hex format
I want this script to create the code for BBcodes for forums NOT by HTML
So RGB format won’t work there

For More Details :
This is my script

[code=html]<HTML>
<HEAD>

</HEAD>
<BODY>
Example
<Div id=”code”>

</Div>
<form name=”codeform”>
HTML Code
<Textarea name=”codeHTML” style=”width:100% ; height:125px”></Textarea><br><br><br>
BB Code
<Textarea name=”codeBB” style=”width:100% ; height:125px”></Textarea>
</form>
<Script>
// The First Color
var r1 = 255
var g1 = 0
var b1 = 0

// The Second Color
var r2 = 0
var g2 = 255
var b2 = 0

var word = “Egypt Egypt Egypt Egypt Egypt Egypt Egypt Egypt Egypt Egypt”
var length = word.length

var r3 = (r2 – r1)/length
var g3 = (g2 – g1)/length
var b3 = (b2 – b1)/length

var i=0
while(i<=length) {
document.all.code.innerHTML+= “<a style=’color:rgb(“+r1+”,”+g1+”,”+b1+”)’>”+word.charAt(i)+”</a>”
document.codeform.codeHTML.value+= “<a style=’color:rgb(“+r1+”,”+g1+”,”+b1+”)’>”+word.charAt(i)+”</a>”
// This BBCode is invalid so HEX format needed !!
document.codeform.codeBB.value+= “[color:rgb(“+r1+”,”+g1+”,”+b1+”)’]”+word.charAt(i)+”[/color]”
r1+=r3
g1+=g3
b1+=b3
i++
}

</Script>
</BODY>
</HTML>
[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJul 07.2006 — Number.prototype.toHex = function(len) {
if(typeof len != "number") len = 6;
var v = "0000000000000000" + (parseInt(this.toString()).toString(16).toUpperCase());
return v.substr(v.length-len,len);
}
var red = 15;
var green = 111;
var blue = 240;
var hexColor = '#' + (red.toHex(2)) + (green.toHex(2)) + (blue.toHex(2));
alert(hexColor);
Copy linkTweet thisAlerts:
@RadianauthorJul 07.2006 — ThanQ Very much , Perfect (Y)

But I have another Question

What about from hex to RGB

color.toRGB ??
Copy linkTweet thisAlerts:
@phpnoviceJul 07.2006 — Just a little hint:

var decColor = parseInt(hexColor.substr(1), 16);

alert(decColor);
Copy linkTweet thisAlerts:
@phpnoviceJul 07.2006 — A note about what I posted originally... You can also do it this way (which should also help to understand the way to reverse it, too):

var red = 15;

var green = 111;

var blue = 240;

var decColor = (red * 65536) + (green * 256) + blue;

var hexColor = '#' + (decColor.toHex(6));

alert(hexColor);
Copy linkTweet thisAlerts:
@phpnoviceJul 07.2006 — OK, here's the full roundabout -- using the new method previously posted:

(PHP tags are bounding this JavaScript for coloring only.)
[code=php]var rgbColor = [15, 111, 240];
var hexColor = '#' + (rgbColor[0].toHex(2)) + (rgbColor[1].toHex(2)) + (rgbColor[2].toHex(2));
alert("rgb(" + rgbColor + ") = " + hexColor);
// ...or...
var decColor = (rgbColor[0] * 65536) + (rgbColor[1] * 256) + rgbColor[2];
var hexColor = '#' + (decColor.toHex(6));
alert("dec(" + decColor + ") = " + hexColor);
// ...reverse...
var r, decColor = parseInt(hexColor.substr(1), 16);
var rgbColor = [r=Math.floor(decColor / 65536), Math.floor((decColor-(r*65536)) / 256), (decColor & 255)];
alert(hexColor + " = rgb(" + rgbColor + ")");[/code]
×

Success!

Help @Radian 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...