Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function getLuminance($val) {
- $hex = $val;
- list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
- $red = 0.2126 * ($r/255);
- $green = 0.7152 * ($g/255);
- $blue = 0.0772 * ($b/255);
- if ( $red + $green + $blue <= .5 ){
- return 'light-text';
- } else {
- return 'dark-text';
- }
- }
- ?>
- //Usage:
- <div class="<?php echo getLuminance('#ffffff'); ?>"></div>
- <div class="<?php echo getLuminance('#000000'); ?>"></div>
- //Results in:
- <div class="dark-text"></div>
- <div class="light-text"></div>
Add Comment
Please, Sign In to add comment