Guest User

Untitled

a guest
Feb 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. <?php
  2. function getLuminance($val) {
  3. $hex = $val;
  4. list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
  5. $red = 0.2126 * ($r/255);
  6. $green = 0.7152 * ($g/255);
  7. $blue = 0.0772 * ($b/255);
  8.  
  9. if ( $red + $green + $blue <= .5 ){
  10. return 'light-text';
  11. } else {
  12. return 'dark-text';
  13. }
  14. }
  15. ?>
  16.  
  17. //Usage:
  18. <div class="<?php echo getLuminance('#ffffff'); ?>"></div>
  19. <div class="<?php echo getLuminance('#000000'); ?>"></div>
  20.  
  21. //Results in:
  22. <div class="dark-text"></div>
  23. <div class="light-text"></div>
Add Comment
Please, Sign In to add comment