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
1.31 K Views
5 Score
3 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 (3votes)
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
C
PHP Customize the default WordPress Image Compression

WordPress uses its own compression for images that are uploaded directly into WordPress. This ha ...

Read More
R
WordPress Remove the URL field in the WordPress comments form

As we know, comments are often misused by website visitors to get external links. Outgoing links ...

Read More
L
WooCommerce Load WooCommerce Templates from Plugin files

By adding a ../wp-content/themes/my-theme/woocommerce/ folder to a WordPress theme, WooCommerce ...

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-2022 - 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