• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: A UDT to convert HEX color to RGB
PostPosted: Thu Apr 11, 2019 12:27 am 
Offline
Forum Members
Forum Members
User avatar

Joined: Wed Aug 29, 2007 4:36 am
Posts: 246
Location: Sydney Australia
UDT titled hex2rgb:
\$1:
function hex2rgb($hex, $alpha = false) {
   $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;
   }
   return implode(array_keys($rgb)) . '(' . implode(', ', $rgb) . ')';
}
echo hex2rgb($params['hex'], $params['alpha']);


And the call to it:
\$1:
{hex2rgb hex='#28a745' alpha=0.5}
or
{hex2rgb hex='#28a745'}


And the output:
\$1:
rgba(40, 167, 69, 0.5)
or
rgb(40, 167, 69)


Works with 3 digit and 6 digit HEX colors.
Works with or without ALPHA channel.
Hope it's useful to someone.

Cheers
Simon66


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: A UDT to convert HEX color to RGB
PostPosted: Thu Apr 11, 2019 2:55 am 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Wed Feb 25, 2009 4:25 am
Posts: 813
Location: Victoria, BC
Nice! Have you tried calling it in a stylesheet too?

_________________
Not getting the answer you need? CMSMS support options


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: A UDT to convert HEX color to RGB
PostPosted: Thu Apr 11, 2019 4:58 am 
Offline
Forum Members
Forum Members
User avatar

Joined: Wed Aug 29, 2007 4:36 am
Posts: 246
Location: Sydney Australia
I hadn't thought of it.
I came up with this UDT so I could to use the colorpicker in Custom Global Settings (CustomGS) for changing the color of the 'filter_colorize="r,g,b[,alpha]"' in CGSmartImage.

I've created a section where an Editor can upload & custom colorize a photo.

I'll play around with it and see what else it can do.

Cheers
Simon66


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: A UDT to convert HEX color to RGB
PostPosted: Fri Apr 12, 2019 11:42 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Wed Aug 29, 2007 4:36 am
Posts: 246
Location: Sydney Australia
Thanks to Jo Morg it's been updated to use multiple times on a page.

\$1:
 $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) . ')';


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
A2 Hosting