Advertisement
Guest User

Untitled

a guest
Feb 28th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.91 KB | None | 0 0
  1. <?php
  2.  
  3. header('Content-Type: text/plain; charset=UTF-8');
  4.  
  5. // https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Country%20data%20templates%20of%20countries&cmlimit=300&cmprop=title
  6. // fetched February 28, 2014, ~14:10
  7.  
  8. $country_names = array(
  9.     'Afghanistan',
  10.     'Albania',
  11.     'Algeria',
  12.     'Andorra',
  13.     'Angola',
  14.     'Antigua and Barbuda',
  15.     'Argentina',
  16.     'Armenia',
  17.     'Australia',
  18.     'Austria',
  19.     'Azerbaijan',
  20.     'Bahamas',
  21.     'Bahrain',
  22.     'Bangladesh',
  23.     'Barbados',
  24.     'Belarus',
  25.     'Belgium',
  26.     'Belize',
  27.     'Benin',
  28.     'Bhutan',
  29.     'Bolivia',
  30.     'Bosnia and Herzegovina',
  31.     'Botswana',
  32.     'Brazil',
  33.     'Brunei',
  34.     'Bulgaria',
  35.     'Burkina Faso',
  36.     'Burundi',
  37.     'Cambodia',
  38.     'Cameroon',
  39.     'Canada',
  40.     'Cape Verde',
  41.     'Central African Republic',
  42.     'Chad',
  43.     'Chile',
  44.     'China',
  45.     'Colombia',
  46.     'Comoros',
  47.     'Costa Rica',
  48.     'Ivory Coast',
  49.     'Croatia',
  50.     'Cuba',
  51.     'Cyprus',
  52.     'Czech Republic',
  53.     'Democratic Republic of the Congo',
  54.     'Denmark',
  55.     'Djibouti',
  56.     'Dominica',
  57.     'Dominican Republic',
  58.     'Ecuador',
  59.     'Egypt',
  60.     'El Salvador',
  61.     'Equatorial Guinea',
  62.     'Eritrea',
  63.     'Estonia',
  64.     'Ethiopia',
  65.     'Fiji',
  66.     'Finland',
  67.     'France',
  68.     'Gabon',
  69.     'Gambia',
  70.     'Georgia',
  71.     'Germany',
  72.     'Ghana',
  73.     'Greece',
  74.     'Grenada',
  75.     'Guatemala',
  76.     'Guinea',
  77.     'Guinea-Bissau',
  78.     'Guyana',
  79.     'Haiti',
  80.     'Honduras',
  81.     'Hungary',
  82.     'Iceland',
  83.     'India',
  84.     'Indonesia',
  85.     'Iran',
  86.     'Iraq',
  87.     'Ireland',
  88.     'Republic of Ireland',
  89.     'Israel',
  90.     'Italy',
  91.     'Jamaica',
  92.     'Japan',
  93.     'Jordan',
  94.     'Kazakhstan',
  95.     'Kenya',
  96.     'Kiribati',
  97.     'Kuwait',
  98.     'Kyrgyzstan',
  99.     'Laos',
  100.     'Latvia',
  101.     'Lebanon',
  102.     'Lesotho',
  103.     'Liberia',
  104.     'Libya',
  105.     'Liechtenstein',
  106.     'Lithuania',
  107.     'Luxembourg',
  108.     'Macedonia',
  109.     'Madagascar',
  110.     'Malawi',
  111.     'Malaysia',
  112.     'Maldives',
  113.     'Mali',
  114.     'Malta',
  115.     'Marshall Islands',
  116.     'Mauritania',
  117.     'Mauritius',
  118.     'Mexico',
  119.     'Federated States of Micronesia',
  120.     'Moldova',
  121.     'Monaco',
  122.     'Mongolia',
  123.     'Montenegro',
  124.     'Morocco',
  125.     'Mozambique',
  126.     'Burma',
  127.     'Myanmar',
  128.     'Namibia',
  129.     'Nauru',
  130.     'Nepal',
  131.     'Kingdom of the Netherlands',
  132.     'New Zealand',
  133.     'Nicaragua',
  134.     'Niger',
  135.     'Nigeria',
  136.     'North Korea',
  137.     'Norway',
  138.     'Oman',
  139.     'Pakistan',
  140.     'Palau',
  141.     'Palestine',
  142.     'Panama',
  143.     'Papua New Guinea',
  144.     'Paraguay',
  145.     'Peru',
  146.     'Philippines',
  147.     'Poland',
  148.     'Portugal',
  149.     'Qatar',
  150.     'Republic of the Congo',
  151.     'Romania',
  152.     'Russia',
  153.     'Rwanda',
  154.     'Saint Kitts and Nevis',
  155.     'Saint Lucia',
  156.     'Saint Vincent and the Grenadines',
  157.     'Samoa',
  158.     'San Marino',
  159.     'São Tomé and Príncipe',
  160.     'Saudi Arabia',
  161.     'Senegal',
  162.     'Serbia',
  163.     'Seychelles',
  164.     'Sierra Leone',
  165.     'Singapore',
  166.     'Slovakia',
  167.     'Slovenia',
  168.     'Solomon Islands',
  169.     'Somalia',
  170.     'South Africa',
  171.     'South Korea',
  172.     'South Sudan',
  173.     'Spain',
  174.     'Sri Lanka',
  175.     'Sudan',
  176.     'Suriname',
  177.     'Swaziland',
  178.     'Sweden',
  179.     'Switzerland',
  180.     'Syria',
  181.     'Tajikistan',
  182.     'Tanzania',
  183.     'Thailand',
  184.     'Timor-Leste',
  185.     'Togo',
  186.     'Tonga',
  187.     'Trinidad and Tobago',
  188.     'Tunisia',
  189.     'Turkey',
  190.     'Turkmenistan',
  191.     'Tuvalu',
  192.     'Uganda',
  193.     'Ukraine',
  194.     'United Arab Emirates',
  195.     'United Kingdom',
  196.     'United States',
  197.     'Uruguay',
  198.     'Uzbekistan',
  199.     'Vanuatu',
  200.     'Vatican City',
  201.     'Venezuela',
  202.     'Vietnam',
  203.     'Yemen',
  204.     'Zambia',
  205.     'Zimbabwe',
  206. );
  207.  
  208. // failed automatic automatic (downloaded manually):
  209.  
  210. $failed_fetching = array(
  211.     'Bahamas',
  212.     'Central African Republic',
  213.     'China',
  214.     'Comoros',
  215.     'Ivory Coast',
  216.     'Czech Republic',
  217.     'Democratic Republic of the Congo',
  218.     'Dominican Republic',
  219.     'Gambia',
  220.     'Republic of Ireland',
  221.     'Marshall Islands',
  222.     'Federated States of Micronesia',
  223.     'Burma',
  224.     'Kingdom of the Netherlands',
  225.     'Philippines',
  226.     'Republic of the Congo',
  227.     'São Tomé and Príncipe',
  228.     'Solomon Islands',
  229.     'Timor-Leste',
  230.     'United Arab Emirates',
  231.     'United Kingdom',
  232.     'United States',
  233.     'Vatican City',
  234. );
  235.  
  236. // echo implode(' SVG, ! Wikipedia file flag of ', $failed_fetching);
  237. // ! 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
  238.  
  239. abstract class Flag
  240. {
  241.     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/
  242.     {
  243.         $oldR = $r;
  244.         $oldG = $g;
  245.         $oldB = $b;
  246.  
  247.         $r /= 255;
  248.         $g /= 255;
  249.         $b /= 255;
  250.  
  251.         $max = max( $r, $g, $b );
  252.         $min = min( $r, $g, $b );
  253.  
  254.         $h;
  255.         $s;
  256.         $l = ( $max + $min ) / 2;
  257.         $d = $max - $min;
  258.  
  259.             if( $d == 0 ){
  260.                 $h = $s = 0; // achromatic
  261.             } else {
  262.                 $s = $d / ( 1 - abs( 2 * $l - 1 ) );
  263.  
  264.             switch( $max ){
  265.                     case $r:
  266.                         $h = 60 * fmod( ( ( $g - $b ) / $d ), 6 );
  267.                             if ($b > $g) {
  268.                             $h += 360;
  269.                         }
  270.                         break;
  271.  
  272.                     case $g:
  273.                         $h = 60 * ( ( $b - $r ) / $d + 2 );
  274.                         break;
  275.  
  276.                     case $b:
  277.                         $h = 60 * ( ( $r - $g ) / $d + 4 );
  278.                         break;
  279.                 }
  280.         }
  281.  
  282.         return array( round( $h, 2 ), round( $s, 2 ), round( $l, 2 ) );
  283.     }
  284.  
  285.     static function generate_colour($r = 0, $g = 0, $b = 0)
  286.     {
  287.         list($h, $s, $l, ) = self::RGB_to_HSL($r, $g, $b);
  288.  
  289.         return array(
  290.             'h' => $h,
  291.             's' => $s,
  292.             'l' => $l,
  293.             'r' => $r,
  294.             'g' => $g,
  295.             'b' => $b,
  296.         );
  297.     }
  298.  
  299.     /* function fetch_image() // obviously not functional in this form
  300.     {
  301.         $n = str_replace(' ', '_', $n);
  302.         $p = file_get_contents('https://en.wikipedia.org/wiki/File:Flag_of_' . $n . '.svg');
  303.         preg_match_all('#nominally ([\d,]+) × ([\d,]+) pixels#', $p, $m);
  304.         $w = str_replace(',', '', $m[1][0]);
  305.         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);
  306.         $f = 'https:' . $m[0][0];
  307.         if(@$m[0][0]) file_put_contents('internals/' . $n . '.png', file_get_contents($f));
  308.         else printf("Failed %s!\n", $n);
  309.     } */
  310.  
  311.     const IMAGES_DIR = 'internals_resized';
  312.     const MOST_COMMON_COLOURS_NUMBER = 20;
  313.     const PIXEL_SAMPLING_STEP = 1;
  314.     const SORTING_COMPONENT = 'h';
  315.     const BLACK_TEXT_LIGHTNESS = .3;
  316.     const REPORT = true;
  317.  
  318.     function __construct($name)
  319.     {
  320.         $this -> name = (string)$name;
  321.         $this -> wiki_name = str_replace(' ', '_', $this -> name);
  322.         $this -> file_name = self::IMAGES_DIR . DIRECTORY_SEPARATOR . $this -> wiki_name . '.png';
  323.  
  324.         if(!is_readable($this -> file_name)) throw new Exception;
  325.  
  326.         $this -> average_colour = self::generate_colour();
  327.         $this -> most_common_colours = array();
  328.     }
  329.  
  330.     function read()
  331.     {
  332.         if(self::REPORT) printf("Reading %s.\n", $this -> name);
  333.  
  334.         $handler = imagecreatefrompng($this -> file_name); // not a property for performance (?)
  335.         list($width, $height) = getimagesize($this -> file_name);
  336.         $this -> width_height_ratio = $width / $height;
  337.         $r_sum = $g_sum = $b_sum = 0;
  338.         $pixel_count = 0;
  339.         $x = $y = 0;
  340.  
  341.         while($y < $height)
  342.         {
  343.             $x = 0;
  344.  
  345.             while($x < $width)
  346.             {
  347.                 $current_colour = imagecolorat($handler, $x, $y);
  348.  
  349.                 if($other_method = imagecolorsforindex($handler, $current_colour))
  350.                 {
  351.                     // http://php.net/manual/en/function.imagecolorat.php#76819
  352.                     $r = $other_method['red'];
  353.                     $g = $other_method['green'];
  354.                     $b = $other_method['blue'];
  355.                 }
  356.                 else
  357.                 {
  358.                     // http://pl1.php.net/imagecolorat#refsect1-function.imagecolorat-examples
  359.                     list($r, $g, $b, ) = array(
  360.                         ($current_colour >> 16) & 0xFF,
  361.                         ($current_colour >>  8) & 0xFF,
  362.                         ($current_colour >>  0) & 0xFF,
  363.                     );
  364.                 }
  365.  
  366.                 $r_sum += $r;
  367.                 $g_sum += $g;
  368.                 $b_sum += $b;
  369.  
  370.                 $common_colours_key = 'string_' . (($b << 0) + ($g << 8) + ($r << 16)); // to preserve the keys in sorting
  371.                 @$this -> most_common_colours[$common_colours_key]++;
  372.  
  373.                 $pixel_count++;
  374.                 $x += self::PIXEL_SAMPLING_STEP;
  375.             }
  376.  
  377.             $y += self::PIXEL_SAMPLING_STEP;
  378.         }
  379.  
  380.         $r_sum /= $pixel_count;
  381.         $g_sum /= $pixel_count;
  382.         $b_sum /= $pixel_count;
  383.  
  384.         $this -> average_colour = self::generate_colour($r_sum, $g_sum, $b_sum);
  385.         arsort($this -> most_common_colours);
  386.         $this -> most_common_colours = array_slice($this -> most_common_colours, 0, self::MOST_COMMON_COLOURS_NUMBER);
  387.     }
  388.  
  389.     static function bw($l)
  390.     {
  391.         return ($l > self::BLACK_TEXT_LIGHTNESS) ? '#000' : '#FFF';
  392.     }
  393.  
  394.     abstract function cell_header();
  395.     abstract function cell_h();
  396.     abstract function cell_s();
  397.     abstract function cell_l();
  398.     abstract function cell_r();
  399.     abstract function cell_g();
  400.     abstract function cell_b();
  401. }
  402.  
  403. class HTMLFlag extends Flag
  404. {
  405.     function cell_header()
  406.     {
  407.         printf("<th style='background-color: rgb(%d, %d, %d); color: %s;'> <img src='%s' style='width: %.1fem; height: %.1fem;'> %s",
  408.             $this -> average_colour['r'],
  409.             $this -> average_colour['g'],
  410.             $this -> average_colour['b'],
  411.             $this -> bw($this -> average_colour['l']),
  412.             $this -> file_name,
  413.             3 * $this -> width_height_ratio,
  414.             3,
  415.             $this -> name
  416.         );
  417.     }
  418.  
  419.     function cell_h()
  420.     {
  421.         printf("<td style='background-color: hsl(%d, %d%%, %d%%);'>%d°",
  422.             $this -> average_colour['h'],
  423.             50,
  424.             50,
  425.             $this -> average_colour['h']
  426.         );
  427.     }
  428.  
  429.     function cell_s()
  430.     {
  431.         printf("<td style='background-color: hsl(%d, %d%%, %d%%);'>%d%%",
  432.             $this -> average_colour['h'],
  433.             $this -> average_colour['s'] * 100,
  434.             50,
  435.             $this -> average_colour['s'] * 100
  436.         );
  437.     }
  438.  
  439.     function cell_l()
  440.     {
  441.         printf("<td style='background-color: hsl(%d, %d%%, %d%%); color: %s;'>%d%%",
  442.             $this -> average_colour['h'],
  443.             50,
  444.             $this -> average_colour['l'] * 100,
  445.             $this -> bw($this -> average_colour['l']),
  446.             $this -> average_colour['l'] * 100
  447.         );
  448.     }
  449.  
  450.     function cell_r() { printf("<td>%d%%", $this -> average_colour['r'] * 100 / 255); }
  451.     function cell_g() { printf("<td>%d%%", $this -> average_colour['g'] * 100 / 255); }
  452.     function cell_b() { printf("<td>%d%%", $this -> average_colour['b'] * 100 / 255); }
  453. }
  454.  
  455. class WikimarkupFlag extends Flag
  456. {
  457.     function cell_header()
  458.     {
  459.         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",
  460.             $this -> average_colour['r'],
  461.             $this -> average_colour['g'],
  462.             $this -> average_colour['b'],
  463.             $this -> bw($this -> average_colour['l']),
  464.             $this -> name,
  465.             $this -> name,
  466.             $this -> name
  467.         );
  468.     }
  469.  
  470.     function cell_h()
  471.     {
  472.         printf("| style='background-color: hsl(%d, %d%%, %d%%);' | %d°\n",
  473.             $this -> average_colour['h'],
  474.             50,
  475.             50,
  476.             $this -> average_colour['h']
  477.         );
  478.     }
  479.  
  480.     function cell_s()
  481.     {
  482.         printf("| style='background-color: hsl(%d, %d%%, %d%%);' | %d%%\n",
  483.             $this -> average_colour['h'],
  484.             $this -> average_colour['s'] * 100,
  485.             50,
  486.             $this -> average_colour['s'] * 100
  487.         );
  488.     }
  489.  
  490.     function cell_l()
  491.     {
  492.         printf("| style='background-color: hsl(%d, %d%%, %d%%); color: %s;' | %d%%\n",
  493.             $this -> average_colour['h'],
  494.             50,
  495.             $this -> average_colour['l'] * 100,
  496.             $this -> bw($this -> average_colour['l']),
  497.             $this -> average_colour['l'] * 100
  498.         );
  499.     }
  500.  
  501.     function cell_r() { printf("| %d%%\n", $this -> average_colour['r'] * 100 / 255); }
  502.     function cell_g() { printf("| %d%%\n", $this -> average_colour['g'] * 100 / 255); }
  503.     function cell_b() { printf("| %d%%\n", $this -> average_colour['b'] * 100 / 255); }
  504. }
  505.  
  506. $flags = array();
  507.  
  508. foreach($country_names as $name)
  509. {
  510.     try
  511.     {
  512.         $flags[] = new WikimarkupFlag($name);
  513.     }
  514.     catch(Exception $e) { continue; }
  515. }
  516.  
  517. foreach($flags as $flag)
  518. {
  519.     try
  520.     {
  521.         $flag -> read();
  522.     }
  523.     catch(Exception $e) { die; }
  524. }
  525.  
  526. if(sizeof($flags) !== sizeof($country_names)) die("List lacking.\n");
  527.  
  528. //$flags = array_slice($flags, 0, 20);
  529.  
  530. uasort($flags, function($a, $b)
  531. {
  532.     return ((int)$a -> average_colour[Flag::SORTING_COMPONENT] === (int)$b -> average_colour[Flag::SORTING_COMPONENT])
  533.         ? strcmp($a -> name, $b -> name)
  534.         : ($a -> average_colour[Flag::SORTING_COMPONENT] > $b -> average_colour[Flag::SORTING_COMPONENT]);
  535. });
  536.  
  537. file_put_contents('flags.php', "<?php\n\n " . var_export($flags, true) . "; \n\n?>");
  538.  
  539. /*
  540. <!DOCTYPE html>
  541. <meta charset='UTF-8'>
  542. <style>
  543. # { text-align: center; }
  544. thead th { padding: .5em; }
  545. thead th[title] { width: 2em; cursor: help; }
  546. tbody th { padding: 2em; font-size: smaller; max-width: 8em; }
  547. tbody th img { display: block; margin: auto; }
  548. </style>
  549. <table border='1px'>
  550. <thead>
  551. <tr>
  552. <th>Country
  553. <th title='hue'>H
  554. <th title='saturation'>S
  555. <th title='lightness'>L
  556. <th title='red'>R
  557. <th title='green'>G
  558. <th title='blue'>B
  559. <tbody>
  560. <!-- [...] -->
  561. </table>
  562. */
  563.  
  564. ?>
  565. {| class='wikitable sortable' style='text-align: center;'
  566. |+ Composition of national flags' average colours <br> ([[HSL and HSV|hue, saturation, lightness]], [[RGB color model|red, green, blue]])
  567. |-
  568. ! class='unsortable' | flag
  569. ! country
  570. ! data-sort-type='number' style='width: 3em;' | [[HSL and HSV|H]]
  571. ! data-sort-type='number' style='width: 3em;' | [[HSL and HSV|S]]
  572. ! data-sort-type='number' style='width: 3em;' | [[HSL and HSV|L]]
  573. ! data-sort-type='number' style='width: 3em;' | [[RGB color model|R]]
  574. ! data-sort-type='number' style='width: 3em;' | [[RGB color model|G]]
  575. ! data-sort-type='number' style='width: 3em;' | [[RGB color model|B]]
  576. <?foreach($flags as $flag):?>
  577. |-
  578. <?$flag -> cell_header()?>
  579. <?$flag -> cell_h()?>
  580. <?$flag -> cell_s()?>
  581. <?$flag -> cell_l()?>
  582. <?$flag -> cell_r()?>
  583. <?$flag -> cell_g()?>
  584. <?$flag -> cell_b()?>
  585. <?endforeach?>
  586. |}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement