RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (5.84%*) • Fixed: 5.39% (5.84%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.19%*) • Investment PI: 5.55% (6.02%*)

Convert Colours Between their Hexadecimal and RGB Values with PHP

Convert Colours Between their Hexadecimal and RGB Values with PHP

The PHP functions on this page will permit you to convert hexadecimal values to their RGB value, or vice versa. Both the Hexadecimal and RGB values have very clear patters in how they're manufactured.

About Hex and RGB Values

The RGB colour model is a model in which red, green, and blue light are added together in various ways to reproduce a broad array of colours. The RGB name comes from the first letter of its primary colours: Red, Blue, and Green. Each of the three values should be between 0 and 256 to create a 6-digit string (with single digit values preceded by a zero).

In math and computer science we'll often see colours represented by a base-16 hexadecimal value ([0-9a-f]{6}). If you're interested in learning about hex for the first time, a really sensible and visual place to start is MathIsFun ... they have a little animation that visually reinforces the principles.

When you use three (hex) characters in the web world for colours it's important that we precede each single character by the same value to form a 6-character #hexdec string (the 3-digit shorthand form reduces the palette to 4,096 colours, equivalent of 12-bit colour as opposed to 24-bit colour using the whole six-digit form, which results in 16,777,216 colours). RGB values are defined in CSS specifications and not generally honored in all browsers - using them should be done with caution.

The PHP Functions

Convert HEX to RGB

1
<?php 
2
<?php
3
/*
4
 Convert Colours Between their Hexadecimal and RGB Values with PHP (HEX to RGB)
5
 http://www.beliefmedia.com/convert-hex-rgb-colour-php
6
*/
7
 
8
function beliefmedia_hex_rgb($hex, $format = 'rgb(%d, %d, %d)') {
9
 
10
  $hex = str_replace('#', '', $hex);
11
 
12
  if (strlen($hex) === 3) {
13
    $rgb = sprintf($format, hexdec($hex[0]), hexdec($hex[1]), hexdec($hex[2]));
14
     } elseif (strlen($hex) === 6) {
15
    $rgb = sprintf($format, hexdec(substr($hex, 0, 2)), hexdec(substr($hex, 2, 2)), hexdec(substr($hex, 4, 2)));
16
 
17
  } else {
18
    $rgb = false;
19
  }
20
 
21
 return $rgb;
22
}
23
 
24
/* Usage */
25
$hex = 'ff0000';
26
echo beliefmedia_hex_rgb($hex);
27
?>

Example returns: rgb(255, 0, 0). You may alter the format in which data is returned via the $format argument.

Convert RGB to HEX

Passing the RGB values to a function presents a few potential problems because of the varied ways in which the values are represented. Using an array keeps it uniform.

1
<?php 
2
<?php
3
/*
4
 Convert Colours Between their Hexadecimal and RGB Values with PHP (RGB to HEX)
5
 http://www.beliefmedia.com/convert-hex-rgb-colour-php
6
*/
7
 
8
function beliefmedia_rgb_hex($rgb) {
9
 return '#' . sprintf('%02x', $rgb['r']) . sprintf('%02x', $rgb['g']) . sprintf('%02x', $rgb['b']);
10
}
11
 
12
/* Usage */
13
$rbgarray = array('r'=>'255', 'g'=>'0', 'b'=>'0');
14
echo beliefmedia_rgb_hex($rbgarray);
15
?>

Example returns: #ff0000.

Considerations

We have a number of color posts scheduled that'll work with data in various ways. They will all be available via the color Tag: color tag.

Download


Title: Convert Colours Between Hexadecimal and RGB Values
Description: Convert Colours Between Hexadecimal and RGB Values with PHP.
  Download • Version 0.2, 610.0B, zip, Category: PHP Code & Snippets

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment