You are here

function slick_color_brightness in Slick Carousel 7.2

Adjusts colors by step/number (0-255).

Parameters

string $hex: A string hex value.

int $steps: An int step between -255 and 255 where negative means darker, and positive lighter.

Return value

string A new hex color string value dependent on steps.

2 calls to slick_color_brightness()
slick_format_field_collection in includes/slick.field_collection.inc
Formats Field collection data.
slick_format_paragraphs in includes/slick.paragraphs.inc
Formats Paragraphs data.

File

includes/slick.extras.inc, line 253
Contains optional functions called by frontend Media, or Field collection.

Code

function slick_color_brightness($hex, $steps) {

  // Steps should be -255 and 255.
  $steps = max(-255, min(255, $steps));

  // Format the hex color string.
  $hex = str_replace('#', '', $hex);
  if (strlen($hex) == 3) {
    $hex = str_repeat(substr($hex, 0, 1), 2) . str_repeat(substr($hex, 1, 1), 2) . str_repeat(substr($hex, 2, 1), 2);
  }

  // Get decimal values.
  $r = hexdec(substr($hex, 0, 2));
  $g = hexdec(substr($hex, 2, 2));
  $b = hexdec(substr($hex, 4, 2));

  // Adjust number of steps and keep it inside 0 to 255.
  $r = max(0, min(255, $r + $steps));
  $g = max(0, min(255, $g + $steps));
  $b = max(0, min(255, $b + $steps));
  $r_hex = str_pad(dechex($r), 2, '0', STR_PAD_LEFT);
  $g_hex = str_pad(dechex($g), 2, '0', STR_PAD_LEFT);
  $b_hex = str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
  return '#' . $r_hex . $g_hex . $b_hex;
}