UDT can only be used once per page

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
Simon66
Power Poster
Power Poster
Posts: 250
Joined: Wed Aug 29, 2007 4:36 am
Location: Sydney Australia

UDT can only be used once per page

Post by Simon66 »

I created a UDT to convert HEX colors to RGB which works OK:
viewtopic.php?f=4&t=80592

The problem is that I can only use it once per page.
It breaks everything if I use it twice with different HEX colors.

Any ideas?

Simon66
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1922
Joined: Mon Jan 29, 2007 4:47 pm

Re: UDT can only be used once per page

Post by Jo Morg »

The UDT you created has a function declaration in it, so each time you call it you are re-declaring it which is a no no.
Try:

Code: Select all

 $hex = $params['hex'];
 $alpha = $params['alpha'];
   $hex      = str_replace('#', '', $hex);
   $length   = strlen($hex);
   $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0));
   $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0));
   $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0));
   if ( $alpha ) {
      $rgb['a'] = $alpha;
   }
   echo implode(array_keys($rgb)) . '(' . implode(', ', $rgb) . ')';
HTH
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Simon66
Power Poster
Power Poster
Posts: 250
Joined: Wed Aug 29, 2007 4:36 am
Location: Sydney Australia

Re: UDT can only be used once per page

Post by Simon66 »

Thanks Fernando

That works perfectly!

Cheers
Simon66
Post Reply

Return to “Modules/Add-Ons”