public static function toHex($color)
  {
    // Check it it's a RGB color
    if (is_array($color)) {
      if ((count($color) == 3 && array_keys($color) === range(0, 2))
       || (count($color) == 4 && array_keys($color) === range(0, 3))) {
        $red = $color[0];
        $green = $color[1];
        $blue = $color[2];
      } else {
        $red = isset($color['red']) ?: (isset($color['r']) ?: 0);
        $red = isset($color['red'])
          ? $color['red']
          : (isset($color['r']) ? $color['r'] : 0);
        $green = isset($color['green'])
          ? $color['green']
          : (isset($color['g']) ? $color['g'] : 0);
        $blue = isset($color['blue'])
          ? $color['blue']
          : (isset($color['b']) ? $color['b'] : 0);
      }
      return '#'
        .substr('00' . dechex($red), -2)
        .substr('00' . dechex($green), -2)
        .substr('00' . dechex($blue), -2);
    }
    if (is_string($color)) {
      $color = strtolower($color);
      // Check if it's a X11 color
      if (array_key_exists($color, self::$x11) !== false) {
        return static::toHex(self::$x11[$color]);
      }
      // Check if it's an 3-hexadecimal color
      if (preg_match('/^#[\da-f]{3}$/i', $color)) {
        $color = preg_replace('/([\da-f])/i', '$1$1', $color);
      }
      if (preg_match('/^#[\da-f]{6}$/i', $color)) {
        return $color;
      }
    }
    return '#000000';
  }