Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Example Interest Rates: Home Loan Variable: 1.89% (1.9%*) • Home Loan Fixed: 1.84% (2.11%*) • Fixed: 1.84% (2.11%*) • Variable: 1.89% (1.9%*) • Investment IO: 2.05% (4.36%*) • Investment PI: 1.58% (2.72%*)

Return an Inverse Color With PHP

The following PHP function will return an inverse HEX color with PHP. It's handy if you need to print readable text on a colored background. It's not always aesthetically pleasing so it's usually best to use a black, neutral, or white text overlay on a background based on the background color's numeric value.

1
<?php 
2
/*
3
 Return an Inverse Color With PHP
4
 http://www.beliefmedia.com/code/php-snippets/inverse-color-php
5
*/
6
 
7
function beliefmedia_inverse_color($color) {
8
 
9
  $color = str_replace('#', '', $color);
10
  if (strlen($color) != 6) $color = str_repeat(substr($color, 0, 1), 2) . str_repeat(substr($color, 1, 1), 2) . str_repeat(substr($color, 2, 1), 2);
11
 
12
  for ($x = 0; $x < 3; $x++) {
13
    $c = 255 - hexdec(substr($color, (2 * $x), 2));
14
    $c = ($c < 0) ? 0 : dechex($c);
15
    $hex .= (strlen($c) < 2) ? '0' . $c : $c;
16
  }
17
 
18
 return '#' . $hex;
19
}
20
 
21
/* Usage (returns #000000) */
22
echo beliefmedia_inverse_color($color = 'ffffff');
23
 
24
/* Usage (returns #00ffff) */
25
echo beliefmedia_inverse_color($color = 'ff0000');

Other color functions can be found on our blog by way of the color Tag: color tag.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Washington)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment