function test_HEX_to_RGB_exception()
 {
     $invalid_hex_color = ColorUtils::hex_rgb('violet');
     $this->assertEquals('Not a valid hex color.', $invalid_hex_color->getMessage());
     $invalid_color_format = ColorUtils::hex_rgb('1234567');
     $this->assertEquals('Not a valid color format.', $invalid_color_format->getMessage());
 }
Exemple #2
0
 /**
  * Checks if the image is a logo, by checking if the outer pixels all have the same color
  * This method returns true when:
  * - The image is a png or gif
  * - The colors of the outermost pixels are the same color
  * @param string $path
  * @return bool
  */
 public static function IsLogo($path)
 {
     if (!file_exists($path)) {
         return false;
     }
     try {
         $info = getimagesize($path);
         $img = null;
         $transparent = false;
         switch ($info[2]) {
             case IMAGETYPE_GIF:
             case IMAGETYPE_PNG:
                 $img = $info[2] == IMAGETYPE_GIF ? imagecreatefromgif($path) : imagecreatefrompng($path);
                 $rgba = imagecolorat($img, 0, 0);
                 $alpha = ($rgba & 0x7f000000) >> 24;
                 $transparent = $alpha == 127;
                 break;
             case IMAGETYPE_JPEG:
             case IMAGETYPE_JPEG2000:
                 $img = imagecreatefromjpeg($path);
                 break;
             default:
                 // Unknown image type
                 throw new \Exception("invalid image");
         }
         $sameColor = 0;
         $totalPixels = $info[0] * 2 + $info[1] * 2 - 4;
         if ($transparent) {
             // Image has transparency, logo for sure
             return true;
         } else {
             $color = ColorUtils::dec2hex(imagecolorat($img, 0, 0));
             // Check top and bottom line for color differences,
             for ($x = 0; $x < $info[0]; $x++) {
                 if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, $x, 0)), 10, $x, 0)) {
                     $sameColor++;
                 }
                 if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, $x, $info[1] - 1)), 10, $x, $info[1] - 1)) {
                     $sameColor++;
                 }
             }
             // Check left and right line for color differences,
             for ($y = 0; $y < $info[1]; $y++) {
                 if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, 0, $y)), 10, 0, $y)) {
                     $sameColor++;
                 }
                 if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, $info[0] - 1, $y)), 10, $info[0] - 1, $y)) {
                     $sameColor++;
                 }
             }
         }
         imagedestroy($img);
         // 90% of the outer pixels are the same color
         return $sameColor / $totalPixels > 0.9;
     } catch (\Exception $e) {
     }
     return false;
 }
 private function get_color($comment_date, $newest_color = '#444444', $fade_out = TRUE)
 {
     if ($fade_out) {
         $time_span = ($_SERVER['REQUEST_TIME'] - $comment_date) / $this->range_in_seconds;
         $time_span = min($time_span, 1);
         $newest_color = ColorUtils::hex_rgb($newest_color);
         $color = array('r' => self::sanitize_color($newest_color['r'] + $this->color_diff['r'] * $time_span), 'g' => self::sanitize_color($newest_color['g'] + $this->color_diff['g'] * $time_span), 'b' => self::sanitize_color($newest_color['b'] + $this->color_diff['b'] * $time_span));
         return '#' . ColorUtils::rgb_hex($color);
     } else {
         return $newest_color;
     }
 }
Exemple #4
0
 /**
  * Converts an hsv color (array(h,s,v) to it's hex equivalent
  * @param array $hsv
  * @returns string The hex color
  */
 public static function hsv2hex($hsv)
 {
     $rgb = ColorUtils::hsv2rgb($hsv);
     return ColorUtils::rgb2hex($rgb);
 }