RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

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.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

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

Leave a comment