Page 1 of 1

UDT can only be used once per page

Posted: Fri Apr 12, 2019 12:41 pm
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

Re: UDT can only be used once per page

Posted: Fri Apr 12, 2019 1:12 pm
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

Re: UDT can only be used once per page

Posted: Fri Apr 12, 2019 9:12 pm
by Simon66
Thanks Fernando

That works perfectly!

Cheers
Simon66