You are here

public function SassColour::__construct in Sassy 7

Same name and namespace in other branches
  1. 7.3 phpsass/script/literals/SassColour.php \SassColour::__construct()

* Constructs an RGB or HSL color object, optionally with an alpha channel. * RGB values must be between 0 and 255. Saturation and lightness values must * be between 0 and 100. The alpha value must be between 0 and 1. * The colour can be specified as: * + a string that is an SVG colour or of the form #rgb or #rrggbb * + an array with either 'red', 'green', and 'blue' keys, and optionally * an alpha key. * + an array with 'hue', 'saturation', and 'lightness' keys, and optionally * an alpha key. * + an array of red, green, and blue values, and optionally an alpha value. *

Parameters

mixed the colour: * @return SassColour

Overrides SassLiteral::__construct

File

phamlp/sass/script/literals/SassColour.php, line 287

Class

SassColour
SassColour class. A SassScript object representing a CSS colour.

Code

public function __construct($colour) {
  if (is_string($colour)) {
    $colour = strtolower($colour);
    if ($colour === self::TRANSPARENT) {
      $this->red = 0;
      $this->green = 0;
      $this->blue = 0;
      $this->alpha = 0;
    }
    else {
      if (array_key_exists($colour, self::$svgColours)) {
        $colour = self::$svgColours[$colour];
      }
      if (strlen($colour) == 4) {
        preg_match(self::EXTRACT_3, $colour, $matches);
        for ($i = 1; $i < 4; $i++) {
          $matches[$i] = str_repeat($matches[$i], 2);
        }
      }
      else {
        preg_match(self::EXTRACT_6, $colour, $matches);
      }
      if (empty($matches)) {
        throw new SassColourException('Invalid {what}', array(
          '{what}' => 'SassColour string',
        ), SassScriptParser::$context->node);
      }
      $this->red = intval($matches[1], 16);
      $this->green = intval($matches[2], 16);
      $this->blue = intval($matches[3], 16);
      $this->alpha = 1;
    }
  }
  elseif (is_array($colour)) {
    $scheme = $this
      ->assertValid($colour);
    if ($scheme == 'rgb') {
      $this->red = $colour['red'];
      $this->green = $colour['green'];
      $this->blue = $colour['blue'];
      $this->alpha = isset($colour['alpha']) ? $colour['alpha'] : 1;
    }
    elseif ($scheme == 'hsl') {
      $this->hue = $colour['hue'];
      $this->saturation = $colour['saturation'];
      $this->lightness = $colour['lightness'];
      $this->alpha = isset($colour['alpha']) ? $colour['alpha'] : 1;
    }
    else {
      $this->red = $colour[0];
      $this->green = $colour[1];
      $this->blue = $colour[2];
      $this->alpha = isset($colour[3]) ? $colour[3] : 1;
    }
  }
  else {
    throw new SassColourException('{what} must be a {type}', array(
      '{what}' => 'Colour',
      '{type}' => 'array',
    ), SassScriptParser::$context->node);
  }
}