mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Is there code you can use to convert $rgb color codes to hex color codes and vice versa?

$rgb(0,0,127) Turn this into #00007F.

And the other way around. Take a hex color code and convert it back to rgb. I don't mean having a list of all the rgb colors you want and all the hex colors you want and refering to it with the alias. I'm hoping for a tool that will convert these color codes.

Is this possible?

Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
As the HTML value is just the hexadecimal representation of the decimal value, I was going to suggest $base($rgb(0,0,127),10,16), but it reverses the RGB values, so a little more trickery is required. (Anybody know how to force an Endian?)

Basically, the HTML value is just the RGB values in hex, so you just $base() the 3 values, and string them together. So going from the hex string to $rgb() values is simple with a small loop and $base()'n each one.

If you've got the 3 separated values that you plug into $rgb(), then it's easy to go the other way as well (hell, you can swap your R and B values, and $base() the entire thing to 16!). But if you've only got the full decimal number (8323072 in your example), then you've only really got the choice of chopping up a converted value, or breaking out into it's componant values.

Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
alias tohex return $+($base($1,10,16,2),$base($2,10,16,2),$base($3,10,16,2))

Useage: $tohex(r,g,b)

Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
That works great. So how do I do it the other way? I haven't found much information on $base. Thank You.

Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
Originally Posted By: help_file
$base(N,inbase,outbase,zeropad,precision)
Converts number N from inbase to outbase. The last two parameters are optional.
$base(15,10,16) returns F
$base(1.5,10,16) returns 1.8
$base(2,10,16,3) returns 002

That's from the help file..

With the above example, it should be fairly easy to figure out. When coming FROM the HTML values, split it into two characters, then $base() from 16->10. These will be your individual RGB values.

Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Yeah I read it and it made no sense to me, but what you showed me worked. Thank You!

Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
This should do. Valid input is assumed.

Code:
alias tohex return $+($base($1,10,16,2),$base($2,10,16,2),$base($3,10,16,2))

alias torgb {
  tokenize 44 $rgb($regsubex($1,/([\da-f]{6})/i,$base(\1,16,10)))
  return $3 $+ , $+ $2 $+ , $+ $1
}

Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Quote:
hex_ {
if (!$1) { return }
var %hex_ = $iif(# isin $1,$right($1,-1),$1)
return $+($base($left(%hex_,2),16,10),$chr(44),$base($mid(%hex_,3,2),16,10),$chr(44),$base($right(%hex_,2),16,10))
}


This is what I made. Thanks for everyone's help.

Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Ok Im having an issue here. I Use this code to convert hex to rgb. It gives me #,#,#.

So I try to put this in $rgb($hex_(2E2E2E)).

This doesn't work. How would I do this? I haven't figured it out yet.

Joined: Aug 2005
Posts: 1,052
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
Problem is that $rgb needs to be seperated by commas and does not adhere to $identifiers (%vars alone with seperated commas) Im sure theres a loophole

Code:
alias hex_ {
  if (!$1) { return }
  var %hex_ = $iif(# isin $1,$right($1,-1),$1)
  %hex_a = $base($left(%hex_,2),16,10) | %hex_b = $base($mid(%hex_,3,2),16,10) | %hex_c = $base($right(%hex_,2),16,10)
  return $rgb(%hex_a,%hex_b,%hex_c)
}


$rgb call within the hex_ command is suitable with its own seperated val returns


Code:
if $reality > $fiction { set %sanity Sane }
Else { echo -a *voices* }
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
Originally Posted By: DJ_Sol
So I try to put this in $rgb($hex_(2E2E2E)).
This doesn't work. How would I do this? I haven't figured it out yet.

The hex_ alias is returning 3 strings separated by comma, but $rgb is parsing this single return "A" as one parameter - and one parameter is insufficient: $rgb(A,).
Just add evaluation brackets: $rgb( [ $hex_(2E2E2E) ] )

The effect is explained more clear here in short form by qwerty, refering to jaytea's comprehensive article
smile

Joined: Aug 2005
Posts: 1,052
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
i forgot to eval frown


Code:
if $reality > $fiction { set %sanity Sane }
Else { echo -a *voices* }
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
These brackets aren't needed that often - I stuggled alot with similar problems... smile
$eval and [ ] are exchangeable in many situations - the issue here is a good example for their basic difference.

Joined: Aug 2005
Posts: 1,052
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
It's odd lately I keep learning I think people are coming up w/difference unseen situations lately unless I ignored the facts and pay more attention lately.


Code:
if $reality > $fiction { set %sanity Sane }
Else { echo -a *voices* }

Link Copied to Clipboard