<?php /** * This class is used in the work fortheloveoffame.com * It has been reworked to use AJAX for proper time-handling. * Some functions aren't used any longer but remain here for * completeness. * @author Vincent Bruijn <vebruijn@gmail.com>, ax710 * @version 2.0 january 2009 */ class ax710 { /** * @var string the URL to the searchengine * change in case the URL doesn't return a JSON string */ private static $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; /** * Convert amount to CMYK * K: rgb( 35, 31, 32) = 0 * M: rgb(236, 0, 140) = 25 * C: rgb( 0, 174, 239) = 50 * Y: rgb(255, 242, 0) = 75 * W: rgb(255, 255, 255) = 100 * Verloop: K -> M -> C -> Y -> White * * @param integer $amount getal van 0 tot 100 * @return string of type rgb(xxx,xxx,xxx) */ public static function convertToCMYK($amount) { if ((int) $amount > 100 || (int) $amount < 0) { return false; } if ($amount <= 25) { $deltaR = (236 - 35) * ($amount / 25) + 35; $deltaG = 31 - ( 31 - 0) * ($amount / 25); $deltaB = (140 - 32) * ($amount / 25) + 32; } else if ($amount <= 50) { $deltaR = 236 - (236 * (($amount - 25) / 25)); $deltaG = (174 - 0) * (($amount - 25) / 25); $deltaB = (239 - 140) * (($amount - 25) / 25) + 140; } else if ($amount <= 75) { $deltaR = (255 - 0) * (($amount - 50) / 25); $deltaG = (242 - 174) * (($amount - 50) / 25) + 174; $deltaB = 239 - (239 - 0) * (($amount - 50) / 25); } else if ($amount <= 100) { $deltaR = 255; $deltaG = (255 - 242) * (($amount - 75) / 25) + 242; $deltaB = (255) * (($amount - 75) / 25); } return "rgb(" . round($deltaR) . ", " . round($deltaG) . ", " . round($deltaB) . ")"; } /** * This is the 'AJAX' version of the function below. * It doesn't use a whole array but returns just the estimated result as integer. * @param string $artist Name of artist (or anything) * @return integer $position */ public static function getScoreAJAX($artist) { $url = self::$url; $url .= urlencode($artist); $position = 0; $result = @file_get_contents($url); if ($result != false) { $array_result = json_decode($result); $position = (int) $array_result->responseData->cursor->estimatedResultCount; } return $position; } /** * function to retrieve the google estimated result count * per artist * @param array An array of strings of names of artists * @return array A sorted array of elements consisting of a string and an integer between 0 and 100 */ public static function getScoreAndSort(array $artists) { foreach ($artists as $key=>$artist) { $url = self::$url; $url .= urlencode($artist); $top100[$key][0] = (string) $artist; $result = @file_get_contents($url); if ($result != false) { $array_result = json_decode($result); $top100[$key][1] = (int) $array_result->responseData->cursor->estimatedResultCount; $top100[$key][2] = (int) $array_result->responseData->cursor->estimatedResultCount; } else { $top100[$key][1] = (int) $key; } } usort($top100, array("ax710","cmp")); $highest = $top100[0][1]; foreach ($top100 as &$artist) { $score = - (100 * $artist[1] / $highest) + 100; $artist[1] = round($score); } return $top100; } /** * compare function for usort() * @param array key * @param array key * @return integer value used by usort to sort an array */ private static function cmp($a, $b) { if ($a[1] == $b[1]) { return 0; } return ($a[1] > $b[1]) ? -1 : 1; } /** * An example function displaying 100 squares changing * from black to magenta to cyan to yellow to white * @return string $example A string of HTML-elements */ public static function example() { $example = (string) ''; for ($value = 1; $value <= 100; $value++) { $example .= "<div style=\"background-color: " . self::convertToCMYK($value); $example .= "; height: 170px; width: 170px; line-height: 170px; float: left;"; $example .= "text-align: center; margin: 4px; font-family: arial; color: #7f7f7f\">\n"; $example .= $value . "\n</div>\n"; } return $example; } }