Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-Type: text/plain; charset=UTF-8');
- // https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Country%20data%20templates%20of%20countries&cmlimit=300&cmprop=title
- // fetched February 28, 2014, ~14:10
- $country_names = array(
- 'Afghanistan',
- 'Albania',
- 'Algeria',
- 'Andorra',
- 'Angola',
- 'Antigua and Barbuda',
- 'Argentina',
- 'Armenia',
- 'Australia',
- 'Austria',
- 'Azerbaijan',
- 'Bahamas',
- 'Bahrain',
- 'Bangladesh',
- 'Barbados',
- 'Belarus',
- 'Belgium',
- 'Belize',
- 'Benin',
- 'Bhutan',
- 'Bolivia',
- 'Bosnia and Herzegovina',
- 'Botswana',
- 'Brazil',
- 'Brunei',
- 'Bulgaria',
- 'Burkina Faso',
- 'Burundi',
- 'Cambodia',
- 'Cameroon',
- 'Canada',
- 'Cape Verde',
- 'Central African Republic',
- 'Chad',
- 'Chile',
- 'China',
- 'Colombia',
- 'Comoros',
- 'Costa Rica',
- 'Ivory Coast',
- 'Croatia',
- 'Cuba',
- 'Cyprus',
- 'Czech Republic',
- 'Democratic Republic of the Congo',
- 'Denmark',
- 'Djibouti',
- 'Dominica',
- 'Dominican Republic',
- 'Ecuador',
- 'Egypt',
- 'El Salvador',
- 'Equatorial Guinea',
- 'Eritrea',
- 'Estonia',
- 'Ethiopia',
- 'Fiji',
- 'Finland',
- 'France',
- 'Gabon',
- 'Gambia',
- 'Georgia',
- 'Germany',
- 'Ghana',
- 'Greece',
- 'Grenada',
- 'Guatemala',
- 'Guinea',
- 'Guinea-Bissau',
- 'Guyana',
- 'Haiti',
- 'Honduras',
- 'Hungary',
- 'Iceland',
- 'India',
- 'Indonesia',
- 'Iran',
- 'Iraq',
- 'Ireland',
- 'Republic of Ireland',
- 'Israel',
- 'Italy',
- 'Jamaica',
- 'Japan',
- 'Jordan',
- 'Kazakhstan',
- 'Kenya',
- 'Kiribati',
- 'Kuwait',
- 'Kyrgyzstan',
- 'Laos',
- 'Latvia',
- 'Lebanon',
- 'Lesotho',
- 'Liberia',
- 'Libya',
- 'Liechtenstein',
- 'Lithuania',
- 'Luxembourg',
- 'Macedonia',
- 'Madagascar',
- 'Malawi',
- 'Malaysia',
- 'Maldives',
- 'Mali',
- 'Malta',
- 'Marshall Islands',
- 'Mauritania',
- 'Mauritius',
- 'Mexico',
- 'Federated States of Micronesia',
- 'Moldova',
- 'Monaco',
- 'Mongolia',
- 'Montenegro',
- 'Morocco',
- 'Mozambique',
- 'Burma',
- 'Myanmar',
- 'Namibia',
- 'Nauru',
- 'Nepal',
- 'Kingdom of the Netherlands',
- 'New Zealand',
- 'Nicaragua',
- 'Niger',
- 'Nigeria',
- 'North Korea',
- 'Norway',
- 'Oman',
- 'Pakistan',
- 'Palau',
- 'Palestine',
- 'Panama',
- 'Papua New Guinea',
- 'Paraguay',
- 'Peru',
- 'Philippines',
- 'Poland',
- 'Portugal',
- 'Qatar',
- 'Republic of the Congo',
- 'Romania',
- 'Russia',
- 'Rwanda',
- 'Saint Kitts and Nevis',
- 'Saint Lucia',
- 'Saint Vincent and the Grenadines',
- 'Samoa',
- 'San Marino',
- 'São Tomé and Príncipe',
- 'Saudi Arabia',
- 'Senegal',
- 'Serbia',
- 'Seychelles',
- 'Sierra Leone',
- 'Singapore',
- 'Slovakia',
- 'Slovenia',
- 'Solomon Islands',
- 'Somalia',
- 'South Africa',
- 'South Korea',
- 'South Sudan',
- 'Spain',
- 'Sri Lanka',
- 'Sudan',
- 'Suriname',
- 'Swaziland',
- 'Sweden',
- 'Switzerland',
- 'Syria',
- 'Tajikistan',
- 'Tanzania',
- 'Thailand',
- 'Timor-Leste',
- 'Togo',
- 'Tonga',
- 'Trinidad and Tobago',
- 'Tunisia',
- 'Turkey',
- 'Turkmenistan',
- 'Tuvalu',
- 'Uganda',
- 'Ukraine',
- 'United Arab Emirates',
- 'United Kingdom',
- 'United States',
- 'Uruguay',
- 'Uzbekistan',
- 'Vanuatu',
- 'Vatican City',
- 'Venezuela',
- 'Vietnam',
- 'Yemen',
- 'Zambia',
- 'Zimbabwe',
- );
- // failed automatic automatic (downloaded manually):
- $failed_fetching = array(
- 'Bahamas',
- 'Central African Republic',
- 'China',
- 'Comoros',
- 'Ivory Coast',
- 'Czech Republic',
- 'Democratic Republic of the Congo',
- 'Dominican Republic',
- 'Gambia',
- 'Republic of Ireland',
- 'Marshall Islands',
- 'Federated States of Micronesia',
- 'Burma',
- 'Kingdom of the Netherlands',
- 'Philippines',
- 'Republic of the Congo',
- 'São Tomé and Príncipe',
- 'Solomon Islands',
- 'Timor-Leste',
- 'United Arab Emirates',
- 'United Kingdom',
- 'United States',
- 'Vatican City',
- );
- // echo implode(' SVG, ! Wikipedia file flag of ', $failed_fetching);
- // ! Wikipedia file flag of Bahamas SVG, ! Wikipedia file flag of Central African Republic SVG, ! Wikipedia file flag of China SVG, ! Wikipedia file flag of Comoros SVG, ! Wikipedia file flag of Ivory Coast SVG, ! Wikipedia file flag of Czech Republic SVG, ! Wikipedia file flag of Democratic Republic of the Congo SVG, ! Wikipedia file flag of Dominican Republic SVG, ! Wikipedia file flag of Gambia SVG, ! Wikipedia file flag of Republic of Ireland SVG, ! Wikipedia file flag of Marshall Islands SVG, ! Wikipedia file flag of Federated States of Micronesia SVG, ! Wikipedia file flag of Burma SVG, ! Wikipedia file flag of Kingdom of the Netherlands SVG, ! Wikipedia file flag of Philippines SVG, ! Wikipedia file flag of Republic of the Congo SVG, ! Wikipedia file flag of São Tomé and Príncipe SVG, ! Wikipedia file flag of Solomon Islands SVG, ! Wikipedia file flag of Timor-Leste SVG, ! Wikipedia file flag of United Arab Emirates SVG, ! Wikipedia file flag of United Kingdom SVG, ! Wikipedia file flag of United States SVG, ! Wikipedia file flag of Vatican City
- abstract class Flag
- {
- static function RGB_to_HSL($r, $g, $b) // http://www.brandonheyer.com/2013/03/27/convert-hsl-to-rgb-and-rgb-to-hsl-via-php/
- {
- $oldR = $r;
- $oldG = $g;
- $oldB = $b;
- $r /= 255;
- $g /= 255;
- $b /= 255;
- $max = max( $r, $g, $b );
- $min = min( $r, $g, $b );
- $h;
- $s;
- $l = ( $max + $min ) / 2;
- $d = $max - $min;
- if( $d == 0 ){
- $h = $s = 0; // achromatic
- } else {
- $s = $d / ( 1 - abs( 2 * $l - 1 ) );
- switch( $max ){
- case $r:
- $h = 60 * fmod( ( ( $g - $b ) / $d ), 6 );
- if ($b > $g) {
- $h += 360;
- }
- break;
- case $g:
- $h = 60 * ( ( $b - $r ) / $d + 2 );
- break;
- case $b:
- $h = 60 * ( ( $r - $g ) / $d + 4 );
- break;
- }
- }
- return array( round( $h, 2 ), round( $s, 2 ), round( $l, 2 ) );
- }
- static function generate_colour($r = 0, $g = 0, $b = 0)
- {
- list($h, $s, $l, ) = self::RGB_to_HSL($r, $g, $b);
- return array(
- 'h' => $h,
- 's' => $s,
- 'l' => $l,
- 'r' => $r,
- 'g' => $g,
- 'b' => $b,
- );
- }
- /* function fetch_image() // obviously not functional in this form
- {
- $n = str_replace(' ', '_', $n);
- $p = file_get_contents('https://en.wikipedia.org/wiki/File:Flag_of_' . $n . '.svg');
- preg_match_all('#nominally ([\d,]+) × ([\d,]+) pixels#', $p, $m);
- $w = str_replace(',', '', $m[1][0]);
- preg_match_all('#//upload.wikimedia.org/wikipedia/[0-9a-z]+?/thumb/[0-9a-z]+?/[0-9a-z]+?/Flag_of_' . $n . '.svg/' . $w . 'px-Flag_of_' . $n . '.svg.png#', $p, $m);
- $f = 'https:' . $m[0][0];
- if(@$m[0][0]) file_put_contents('internals/' . $n . '.png', file_get_contents($f));
- else printf("Failed %s!\n", $n);
- } */
- const IMAGES_DIR = 'internals_resized';
- const MOST_COMMON_COLOURS_NUMBER = 20;
- const PIXEL_SAMPLING_STEP = 1;
- const SORTING_COMPONENT = 'h';
- const BLACK_TEXT_LIGHTNESS = .3;
- const REPORT = true;
- function __construct($name)
- {
- $this -> name = (string)$name;
- $this -> wiki_name = str_replace(' ', '_', $this -> name);
- $this -> file_name = self::IMAGES_DIR . DIRECTORY_SEPARATOR . $this -> wiki_name . '.png';
- if(!is_readable($this -> file_name)) throw new Exception;
- $this -> average_colour = self::generate_colour();
- $this -> most_common_colours = array();
- }
- function read()
- {
- if(self::REPORT) printf("Reading %s.\n", $this -> name);
- $handler = imagecreatefrompng($this -> file_name); // not a property for performance (?)
- list($width, $height) = getimagesize($this -> file_name);
- $this -> width_height_ratio = $width / $height;
- $r_sum = $g_sum = $b_sum = 0;
- $pixel_count = 0;
- $x = $y = 0;
- while($y < $height)
- {
- $x = 0;
- while($x < $width)
- {
- $current_colour = imagecolorat($handler, $x, $y);
- if($other_method = imagecolorsforindex($handler, $current_colour))
- {
- // http://php.net/manual/en/function.imagecolorat.php#76819
- $r = $other_method['red'];
- $g = $other_method['green'];
- $b = $other_method['blue'];
- }
- else
- {
- // http://pl1.php.net/imagecolorat#refsect1-function.imagecolorat-examples
- list($r, $g, $b, ) = array(
- ($current_colour >> 16) & 0xFF,
- ($current_colour >> 8) & 0xFF,
- ($current_colour >> 0) & 0xFF,
- );
- }
- $r_sum += $r;
- $g_sum += $g;
- $b_sum += $b;
- $common_colours_key = 'string_' . (($b << 0) + ($g << 8) + ($r << 16)); // to preserve the keys in sorting
- @$this -> most_common_colours[$common_colours_key]++;
- $pixel_count++;
- $x += self::PIXEL_SAMPLING_STEP;
- }
- $y += self::PIXEL_SAMPLING_STEP;
- }
- $r_sum /= $pixel_count;
- $g_sum /= $pixel_count;
- $b_sum /= $pixel_count;
- $this -> average_colour = self::generate_colour($r_sum, $g_sum, $b_sum);
- arsort($this -> most_common_colours);
- $this -> most_common_colours = array_slice($this -> most_common_colours, 0, self::MOST_COMMON_COLOURS_NUMBER);
- }
- static function bw($l)
- {
- return ($l > self::BLACK_TEXT_LIGHTNESS) ? '#000' : '#FFF';
- }
- abstract function cell_header();
- abstract function cell_h();
- abstract function cell_s();
- abstract function cell_l();
- abstract function cell_r();
- abstract function cell_g();
- abstract function cell_b();
- }
- class HTMLFlag extends Flag
- {
- function cell_header()
- {
- printf("<th style='background-color: rgb(%d, %d, %d); color: %s;'> <img src='%s' style='width: %.1fem; height: %.1fem;'> %s",
- $this -> average_colour['r'],
- $this -> average_colour['g'],
- $this -> average_colour['b'],
- $this -> bw($this -> average_colour['l']),
- $this -> file_name,
- 3 * $this -> width_height_ratio,
- 3,
- $this -> name
- );
- }
- function cell_h()
- {
- printf("<td style='background-color: hsl(%d, %d%%, %d%%);'>%d°",
- $this -> average_colour['h'],
- 50,
- 50,
- $this -> average_colour['h']
- );
- }
- function cell_s()
- {
- printf("<td style='background-color: hsl(%d, %d%%, %d%%);'>%d%%",
- $this -> average_colour['h'],
- $this -> average_colour['s'] * 100,
- 50,
- $this -> average_colour['s'] * 100
- );
- }
- function cell_l()
- {
- printf("<td style='background-color: hsl(%d, %d%%, %d%%); color: %s;'>%d%%",
- $this -> average_colour['h'],
- 50,
- $this -> average_colour['l'] * 100,
- $this -> bw($this -> average_colour['l']),
- $this -> average_colour['l'] * 100
- );
- }
- function cell_r() { printf("<td>%d%%", $this -> average_colour['r'] * 100 / 255); }
- function cell_g() { printf("<td>%d%%", $this -> average_colour['g'] * 100 / 255); }
- function cell_b() { printf("<td>%d%%", $this -> average_colour['b'] * 100 / 255); }
- }
- class WikimarkupFlag extends Flag
- {
- function cell_header()
- {
- printf("| style='padding: .25em 1em; background-color: rgb(%d, %d, %d); color: %s;' | [[File:Flag of %s.svg|x30px]]\n! style='text-align: left; font-size: smaller;' | [[Flag of %s|%s]]\n",
- $this -> average_colour['r'],
- $this -> average_colour['g'],
- $this -> average_colour['b'],
- $this -> bw($this -> average_colour['l']),
- $this -> name,
- $this -> name,
- $this -> name
- );
- }
- function cell_h()
- {
- printf("| style='background-color: hsl(%d, %d%%, %d%%);' | %d°\n",
- $this -> average_colour['h'],
- 50,
- 50,
- $this -> average_colour['h']
- );
- }
- function cell_s()
- {
- printf("| style='background-color: hsl(%d, %d%%, %d%%);' | %d%%\n",
- $this -> average_colour['h'],
- $this -> average_colour['s'] * 100,
- 50,
- $this -> average_colour['s'] * 100
- );
- }
- function cell_l()
- {
- printf("| style='background-color: hsl(%d, %d%%, %d%%); color: %s;' | %d%%\n",
- $this -> average_colour['h'],
- 50,
- $this -> average_colour['l'] * 100,
- $this -> bw($this -> average_colour['l']),
- $this -> average_colour['l'] * 100
- );
- }
- function cell_r() { printf("| %d%%\n", $this -> average_colour['r'] * 100 / 255); }
- function cell_g() { printf("| %d%%\n", $this -> average_colour['g'] * 100 / 255); }
- function cell_b() { printf("| %d%%\n", $this -> average_colour['b'] * 100 / 255); }
- }
- $flags = array();
- foreach($country_names as $name)
- {
- try
- {
- $flags[] = new WikimarkupFlag($name);
- }
- catch(Exception $e) { continue; }
- }
- foreach($flags as $flag)
- {
- try
- {
- $flag -> read();
- }
- catch(Exception $e) { die; }
- }
- if(sizeof($flags) !== sizeof($country_names)) die("List lacking.\n");
- //$flags = array_slice($flags, 0, 20);
- uasort($flags, function($a, $b)
- {
- return ((int)$a -> average_colour[Flag::SORTING_COMPONENT] === (int)$b -> average_colour[Flag::SORTING_COMPONENT])
- ? strcmp($a -> name, $b -> name)
- : ($a -> average_colour[Flag::SORTING_COMPONENT] > $b -> average_colour[Flag::SORTING_COMPONENT]);
- });
- file_put_contents('flags.php', "<?php\n\n " . var_export($flags, true) . "; \n\n?>");
- /*
- <!DOCTYPE html>
- <meta charset='UTF-8'>
- <style>
- # { text-align: center; }
- thead th { padding: .5em; }
- thead th[title] { width: 2em; cursor: help; }
- tbody th { padding: 2em; font-size: smaller; max-width: 8em; }
- tbody th img { display: block; margin: auto; }
- </style>
- <table border='1px'>
- <thead>
- <tr>
- <th>Country
- <th title='hue'>H
- <th title='saturation'>S
- <th title='lightness'>L
- <th title='red'>R
- <th title='green'>G
- <th title='blue'>B
- <tbody>
- <!-- [...] -->
- </table>
- */
- ?>
- {| class='wikitable sortable' style='text-align: center;'
- |+ Composition of national flags' average colours <br> ([[HSL and HSV|hue, saturation, lightness]], [[RGB color model|red, green, blue]])
- |-
- ! class='unsortable' | flag
- ! country
- ! data-sort-type='number' style='width: 3em;' | [[HSL and HSV|H]]
- ! data-sort-type='number' style='width: 3em;' | [[HSL and HSV|S]]
- ! data-sort-type='number' style='width: 3em;' | [[HSL and HSV|L]]
- ! data-sort-type='number' style='width: 3em;' | [[RGB color model|R]]
- ! data-sort-type='number' style='width: 3em;' | [[RGB color model|G]]
- ! data-sort-type='number' style='width: 3em;' | [[RGB color model|B]]
- <?foreach($flags as $flag):?>
- |-
- <?$flag -> cell_header()?>
- <?$flag -> cell_h()?>
- <?$flag -> cell_s()?>
- <?$flag -> cell_l()?>
- <?$flag -> cell_r()?>
- <?$flag -> cell_g()?>
- <?$flag -> cell_b()?>
- <?endforeach?>
- |}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement