The Must Have for WordPress
The Must Have for WordPress
×
  • Home
December 5, 2019
PHP

Convert HEX color to RGBA color code in PHP

by Martin Jost
No comments
1 min Reading Time
201 Views
5 Score
2 Likes
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp

While working with theme or plugin options, in some cases we need to convert a hex (hexadecimal) color code, which we get from a color picker field to the corresponding RGBA color code, e.g. to get a RGBA color value with the transparency for using in CSS.

How to convert hex to RGB or RGBA

How do I get the RGB or RGBA value if the stored option contains the hexadecimal color code? By passing a HEX color value to the following HEX2RGBA function, you will get the correct output for using in CSS like rgb(255,255,255) or rgba(0,0,0,0.5):

How do we use the function?

Within a template, we can now use the function by calling hex2rgba( $color ); to convert the HEX code to RGB or RGBA. The following example shows how we can use this function:

Last updated: December 5, 2019

Convert HEX color to RGBA color code in PHP Score: 5 / 5 (2votes)
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp
The Author
Published by
Martin Jost

View all posts by Martin Jost

Related Posts
A
WordPress Add WordPress images to the RSS feed

By default, if you have enabled the WordPress RSS feed, images will not be included in the RSS f ...

Read More
C
WordPress Customize or Modify the WordPress Post Excerpt

WordPress comes with own functions, such as get_the_excerpt() or the_excerpt(), to output a shor ...

Read More
S
WordPress Set custom WordPress post format for custom post type

If you’re working with custom post types in WordPress, you may have noticed that all post ...

Read More
Read more
C
Previous Post
Calculate the correct content width for the Gutenberg editor
M
Next Post
Minify and compress Javascript files in PHP
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Imprint
  • Privacy Policy
© 2016-2021 - wphave - The Must Have for WordPress
Login

Lost your password?

RequiredEssential cookies enable basic functions and are necessary for the proper functioning of the website.
  • Name: Cookie Status
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user's web browser allows cookies.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookie_status
  • Duration: 1 Minute
  • Name: Cookie Consent
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user agrees to allow opt-in cookies or not.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookieconsent_analytics, cookieconsent_marketing, cookieconsent_status
  • Duration: 1 Year
  • Name: Like Button
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already gave a like for a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): like_btn_{POST_ID}
  • Duration: 21 Days
  • Name: Post Rating
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already rate a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): rating_{POST_ID}
  • Duration: 7 Days
AnalyticsStatistics cookies collect information anonymously. This information helps us to understand how our visitors use our website.
  • Name: Google Analytics
  • Provider: Google LLC
  • Purpose: Google cookie for website analysis. Generates statistical data on how the visitor uses the website.
  • Privacy Policy: https://policies.google.com/privacy
  • Cookie Name(s): _ga, _gat, _gid
  • Duration: 2 Years