mIRC Homepage
Posted By: DJ_Sol converting $rgb to hex (html) color codes - 09/10/07 01:20 AM
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?
Posted By: Bekar Re: converting $rgb to hex (html) color codes - 09/10/07 01:46 AM
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.
alias tohex return $+($base($1,10,16,2),$base($2,10,16,2),$base($3,10,16,2))

Useage: $tohex(r,g,b)
Posted By: DJ_Sol Re: converting $rgb to hex (html) color codes - 11/10/07 09:59 AM
That works great. So how do I do it the other way? I haven't found much information on $base. Thank You.
Posted By: Bekar Re: converting $rgb to hex (html) color codes - 11/10/07 10:30 AM
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.
Posted By: DJ_Sol Re: converting $rgb to hex (html) color codes - 11/10/07 10:08 PM
Yeah I read it and it made no sense to me, but what you showed me worked. Thank You!
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
}
Posted By: DJ_Sol Re: converting $rgb to hex (html) color codes - 12/10/07 09:56 AM
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.
Posted By: DJ_Sol Re: converting $rgb to hex (html) color codes - 27/11/07 09:47 PM
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.
Posted By: Lpfix5 Re: converting $rgb to hex (html) color codes - 27/11/07 10:04 PM
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
Posted By: Horstl Re: converting $rgb to hex (html) color codes - 27/11/07 10:52 PM
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
Posted By: Lpfix5 Re: converting $rgb to hex (html) color codes - 27/11/07 11:08 PM
i forgot to eval frown
Posted By: Horstl Re: converting $rgb to hex (html) color codes - 27/11/07 11:23 PM
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.
Posted By: Lpfix5 Re: converting $rgb to hex (html) color codes - 28/11/07 04:27 AM
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.
© mIRC Discussion Forums