Posted By

koncept on 03/28/08


Tagged

php hex


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

mb
uisluu
canberkol


Shift Hex Color


 / Published in: PHP
 

See provided example at bottom of code block ...

  1. <?php
  2. /**
  3.  * Color shift a hex value by a specific percentage factor
  4.  *
  5.  * @param string $supplied_hex Any valid hex value. Short forms e.g. #333 accepted.
  6.  * @param string $shift_method How to shift the value e.g( +,up,lighter,>)
  7.  * @param integer $percentage Percentage in range of [0-100] to shift provided hex value by
  8.  * @return string shifted hex value
  9.  * @version 1.0 2008-03-28
  10.  */
  11.  
  12. function hex_shift($supplied_hex,$shift_method,$percentage=50)
  13. {
  14. $shifted_hex_value = null;
  15. $valid_shift_option = FALSE;
  16. $current_set = 1;
  17. $RGB_values = array();
  18. $valid_shift_up_args = array('up','+','lighter','>');
  19. $valid_shift_down_args = array('down','-','darker','<');
  20. $shift_method = strtolower(trim($shift_method));
  21.  
  22. // Check Factor
  23. if(!is_numeric($percentage)||($percentage = (int) $percentage)<0||$percentage>100) {
  24. trigger_error( "Invalid factor", E_USER_ERROR );
  25. }
  26.  
  27. // Check shift method
  28. foreach(array($valid_shift_down_args,$valid_shift_up_args) as $options){
  29. foreach($options as $method) {
  30. if($method == $shift_method) {
  31. $valid_shift_option = !$valid_shift_option;
  32. $shift_method = ($current_set === 1) ? '+' : '-';
  33. break 2;
  34. }
  35. }
  36. ++$current_set;
  37. }
  38.  
  39. if(!$valid_shift_option) {
  40. trigger_error( "Invalid shift method", E_USER_ERROR );
  41. }
  42.  
  43. // Check Hex string
  44. switch(strlen($supplied_hex=(str_replace('#','',trim($supplied_hex))))) {
  45. case 3:
  46. if(preg_match('/^([0-9a-f])([0-9a-f])([0-9a-f])/i',$supplied_hex)) {
  47. $supplied_hex = preg_replace('/^([0-9a-f])([0-9a-f])([0-9a-f])/i',
  48. '\\1\\1\\2\\2\\3\\3',$supplied_hex);
  49. } else {
  50. trigger_error( "Invalid hex value", E_USER_ERROR );
  51. }
  52. break;
  53. case 6:
  54. if(!preg_match('/^[0-9a-f]{2}[0-9a-f]{2}[0-9a-f]{2}$/i',$supplied_hex)) {
  55. trigger_error( "Invalid hex value", E_USER_ERROR );
  56. }
  57. break;
  58. default:
  59. trigger_error( "Invalid hex length", E_USER_ERROR );
  60. }
  61.  
  62. // Start shifting
  63. $RGB_values['R'] = hexdec($supplied_hex{0}.$supplied_hex{1});
  64. $RGB_values['G'] = hexdec($supplied_hex{2}.$supplied_hex{3});
  65. $RGB_values['B'] = hexdec($supplied_hex{4}.$supplied_hex{5});
  66.  
  67. foreach($RGB_values as $c => $v) {
  68. switch($shift_method) {
  69. case '-':
  70. $amount = round(((255-$v)/100)*$percentage)+$v;
  71. break;
  72. case '+':
  73. $amount = $v-round(($v/100)*$percentage);
  74. break;
  75. default:
  76. trigger_error( "Oops. Unexpected shift method", E_USER_ERROR );
  77. }
  78.  
  79. $shifted_hex_value .= $current_value = (
  80. strlen($decimal_to_hex = dechex($amount)) < 2
  81. ) ? '0'.$decimal_to_hex : $decimal_to_hex;
  82. }
  83.  
  84. return '#'.$shifted_hex_value;
  85. }
  86.  
  87. // ---------------------------------------------------
  88. // Example. Black to white fade by an increment of 10%
  89. // ---------------------------------------------------
  90. for ($i=0; $i <= 100; $i+=10) {
  91. echo hex_shift('#000','up',$i),"\n";
  92. }
  93.  
  94. ?>

Report this snippet  

You need to login to post a comment.