Gregan Posted January 22, 2016 Share Posted January 22, 2016 (edited) Hi Everyone, I'm working on a project which will eventually work its way into a website design and came upon a color sorting problem. I found a potential great solution on Stack Overflow using the PHP language but I can't get the code to work. I've never coded in PHP and I've not coded much in years... I was wondering if someone could help me trouble shoot my code below? Here is the stack overflow answer: http://stackoverflow.com/a/31742242/5807915, I didn't ask any further questions over there, that community intimidates me! Here is my set of colors and the code that I've pulled together that I'm trying to get to work in this PHP http://phpfiddle.org/ code editor: <?php $colors = [ [ 'color'=> '# 000000 '], [ 'color'=> '# 97233F '], [ 'color'=> '# 841F27 '], [ 'color'=> '# A71930 '], [ 'color'=> '# EF0107 '], [ 'color'=> '# 94BEE5 '], [ 'color'=> '# CE1141 '], [ 'color'=> '# E13A3E '], [ 'color'=> '# DF4601 '], [ 'color'=> '# 241773 '], [ 'color'=> '# 008348 '], [ 'color'=> '# BD3039 '], [ 'color'=> '# 061922 '], [ 'color'=> '# 00338D '], [ 'color'=> '# 002E62 '], [ 'color'=> '# 8CCCE5 '], [ 'color'=> '# E03A3E '], [ 'color'=> '# 0085CA '], [ 'color'=> '# 1D1160 '], [ 'color'=> '# 034694 '], [ 'color'=> '# 0B162A '], [ 'color'=> '# E3263A '], [ 'color'=> '# CC3433 '], [ 'color'=> '# AF2626 '], [ 'color'=> '# AA002C '], [ 'color'=> '# C6011F '], [ 'color'=> '# FB4F14 '], [ 'color'=> '# 860038 '], [ 'color'=> '# E31937 '], [ 'color'=> '# 8B2942 '], [ 'color'=> '# 91022D '], [ 'color'=> '# 333366 '], [ 'color'=> '# 00285C '], [ 'color'=> '# 1B458F '], [ 'color'=> '# 002244 '], [ 'color'=> '# 007DC5 '], [ 'color'=> '# 006A4E '], [ 'color'=> '# 4D90CD '], [ 'color'=> '# 005A8B '], [ 'color'=> '# ED174C '], [ 'color'=> '# EC1F26 '], [ 'color'=> '# 0C2C56 '], [ 'color'=> '# 003777 '], [ 'color'=> '# 274488 '], [ 'color'=> '# CF0032 '], [ 'color'=> '# C8213F '], [ 'color'=> '# FDB927 '], [ 'color'=> '# 203731 '], [ 'color'=> '# 002D62 '], [ 'color'=> '# F36600 '], [ 'color'=> '# 03202F '], [ 'color'=> '# F5A12D '], [ 'color'=> '# FFC633 '], [ 'color'=> '# 002C5F '], [ 'color'=> '# E31837 '], [ 'color'=> '# 004687 '], [ 'color'=> '# FDBE11 '], [ 'color'=> '# 00A398 '], [ 'color'=> '# BA0021 '], [ 'color'=> '# EF3E42 '], [ 'color'=> '# 00245D '], [ 'color'=> '# 5CBFEB '], [ 'color'=> '# DA020E '], [ 'color'=> '# 0F586C '], [ 'color'=> '# 008E97 '], [ 'color'=> '# 98002E '], [ 'color'=> '# FF6600 '], [ 'color'=> '# 0A2351 '], [ 'color'=> '# 00471B '], [ 'color'=> '# 005083 '], [ 'color'=> '# 002B5C '], [ 'color'=> '# 4F2683 '], [ 'color'=> '# 025736 '], [ 'color'=> '# BF2F38 '], [ 'color'=> '# 122089 '], [ 'color'=> '# FDBB2F '], [ 'color'=> '# 0A2141 '], [ 'color'=> '# 9F8958 '], [ 'color'=> '# 6CADDF '], [ 'color'=> '# 0B2265 '], [ 'color'=> '# 00529B '], [ 'color'=> '# 006BB6 '], [ 'color'=> '# FF5910 '], [ 'color'=> '# 0161AB '], [ 'color'=> '# D50031 '], [ 'color'=> '# E4002B '], [ 'color'=> '# 00B6F1 '], [ 'color'=> '# 003831 '], [ 'color'=> '# A5ACAF '], [ 'color'=> '# 007DC3 '], [ 'color'=> '# 633492 '], [ 'color'=> '# E4173E '], [ 'color'=> '# 004953 '], [ 'color'=> '# F47940 '], [ 'color'=> '# 284898 '], [ 'color'=> '# 001B2D '], [ 'color'=> '# E56020 '], [ 'color'=> '# FDB827 '], [ 'color'=> '# 004812 '], [ 'color'=> '# 005CAB '], [ 'color'=> '# A50531 '], [ 'color'=> '# 724C9F '], [ 'color'=> '# BAC3C9 '], [ 'color'=> '# 05143F '], [ 'color'=> '# AA0000 '], [ 'color'=> '# FD5A1E '], [ 'color'=> '# 0051BA '], [ 'color'=> '# 05535D '], [ 'color'=> '# 4F8A10 '], [ 'color'=> '# ED1A3B '], [ 'color'=> '# 91B0D5 '], [ 'color'=> '# 0546A0 '], [ 'color'=> '# C41E3A '], [ 'color'=> '# EB172B '], [ 'color'=> '# D50A0A '], [ 'color'=> '# 013E7D '], [ 'color'=> '# 092C5C '], [ 'color'=> '# C0111F '], [ 'color'=> '# 134A8E '], [ 'color'=> '# D80016 '], [ 'color'=> '# 001C58 '], [ 'color'=> '# 07346F '], [ 'color'=> '# 12264C '], [ 'color'=> '# CF132B '], [ 'color'=> '# AB0003 '], [ 'color'=> '# 773141 '], [ 'color'=> '# 091453 '], [ 'color'=> '# 60223B '], [ 'color'=> '# 91764B '], [ 'color'=> '# 023474 '], [ 'color'=> '# 7A003C '], [ 'color'=> '# 13274F '], [ 'color'=> '# C4D600 '], [ 'color'=> '# FFC422 '], [ 'color'=> '# BB9753 '], [ 'color'=> '# 0D2B56 '], [ 'color'=> '# E03A3F '], [ 'color'=> '# 565A5C '], [ 'color'=> '# B95915 '], [ 'color'=> '# B1BABF '], [ 'color'=> '# C60C30 '], [ 'color'=> '# FAB383 '], [ 'color'=> '# A9B0B8 '], [ 'color'=> '# 313F49 '], [ 'color'=> '# 20385B '], [ 'color'=> '# D7A22A '], [ 'color'=> '# BEC0C2 '], [ 'color'=> '# FFD200 '], [ 'color'=> '# 8177B7 '], [ 'color'=> '# BCBEC0 '], [ 'color'=> '# FFE600 '], [ 'color'=> '# EEE3C7 '], [ 'color'=> '# 231F20 '], [ 'color'=> '# 8A8D8F '], [ 'color'=> '# F0B83D '], [ 'color'=> '# C4CED3 '], [ 'color'=> '# F9A01B '], [ 'color'=> '# 00539B '], [ 'color'=> '# FF7900 '], [ 'color'=> '# B4975A '], [ 'color'=> '# 5B2B2F '], [ 'color'=> '# 006778 '], [ 'color'=> '# C09A5B '], [ 'color'=> '# 0053A0 '], [ 'color'=> '# D00027 '], [ 'color'=> '# 003263 '], [ 'color'=> '# 005A9C '], [ 'color'=> '# 004689 '], [ 'color'=> '# AFB7BA '], [ 'color'=> '# 552582 '], [ 'color'=> '# FFCE65 '], [ 'color'=> '# FFE500 '], [ 'color'=> '# 7399C6 '], [ 'color'=> '# F58220 '], [ 'color'=> '# 0077C8 '], [ 'color'=> '# B6922E '], [ 'color'=> '# F0EBD2 '], [ 'color'=> '# 00A94F '], [ 'color'=> '# D31145 '], [ 'color'=> '# FFC62F '], [ 'color'=> '# BF2B37 '], [ 'color'=> '# 213770 '], [ 'color'=> '# 00285E '], [ 'color'=> '# F57D31 '], [ 'color'=> '# F58426 '], [ 'color'=> '# 002D72 '], [ 'color'=> '# E6393F '], [ 'color'=> '# 012055 '], [ 'color'=> '# 003087 '], [ 'color'=> '# BBBDBF '], [ 'color'=> '# EFB21E '], [ 'color'=> '# F05133 '], [ 'color'=> '# FDE192 '], [ 'color'=> '# E81828 '], [ 'color'=> '# B18500 '], [ 'color'=> '# D1BD80 '], [ 'color'=> '# EBE72B '], [ 'color'=> '# 013474 '], [ 'color'=> '# 8E9090 '], [ 'color'=> '# 0073CF '], [ 'color'=> '# B3995D '], [ 'color'=> '# F38F20 '], [ 'color'=> '# 005C5C '], [ 'color'=> '# 69BE28 '], [ 'color'=> '# 11568C '], [ 'color'=> '# 211E1F '], [ 'color'=> '# FFC325 '], [ 'color'=> '# 000066 '], [ 'color'=> '# 1B449C '], [ 'color'=> '# A68A26 '], [ 'color'=> '# 34302B '], [ 'color'=> '# 8FBCE6 '], [ 'color'=> '# 4B92DB '], [ 'color'=> '# 003278 '], [ 'color'=> '# 1D2D5C '], [ 'color'=> '# 047A4A '], [ 'color'=> '# 00214E '], [ 'color'=> '# 11225B '], [ 'color'=> '# F7C240 '], [ 'color'=> '# 0168AB '], [ 'color'=> '# EF5225 '], [ 'color'=> '# FFB612 '], [ 'color'=> '# EFE1C6 '], [ 'color'=> '# E3D4AD '], [ 'color'=> '# 9C824A '], ]; foreach ($colors as &$color) { $color['hsl'] = hexToHsl($color['color']); } /** * Convert a hexadecimal color in RGB * @param string $hex * @return array */ function hexToHsl($hex){ list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x"); return rgbToHsl($r, $g, $b); } /** * Convert a RGB color in its HSL value * @param int $r red * @param int $g green * @param int $b blue * @return array */ function rgbToHsl($r, $g, $b) { $r /= 255; $g /= 255; $b /= 255; $max = max($r, $g, $b); $min = min($r, $g, $b); $h = 0; $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('h' => round($h, 2), 's' => round($s, 2), 'l' => round($l, 2)); } usort($colors, function ($a, $b) { //Compare the hue when they are in the same "range" if(!huesAreinSameInterval($a['hsl']['h'],$b['hsl']['h'])){ if ($a['hsl']['h'] < $b['hsl']['h']) return -1; if ($a['hsl']['h'] > $b['hsl']['h']) return 1; } if ($a['hsl']['l'] < $b['hsl']['l']) return 1; if ($a['hsl']['l'] > $b['hsl']['l']) return -1; if ($a['hsl']['s'] < $b['hsl']['s']) return -1; if ($a['hsl']['s'] > $b['hsl']['s']) return 1; return 0; }); /** * Check if two hues are in the same given interval * @param float $hue1 * @param float $hue2 * @param int $interval * @return bool */ function huesAreinSameInterval($hue1, $hue2, $interval = 30){ return (round(($hue1 / $interval), 0, PHP_ROUND_HALF_DOWN) === round(($hue2 / $interval), 0, PHP_ROUND_HALF_DOWN)); } ?> Edited January 24, 2016 by gizmola Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/ Share on other sites More sharing options...
ginerjm Posted January 22, 2016 Share Posted January 22, 2016 Don't have a clue as to what you are attempting but... Have you never worked with arrays before? You are supposedly creating an array with a character index value that is the SAME for every entry. What that means is your array will have only one entry - that being the last one you create. Of course your whole syntax looks strange to me so I may be wrong. I'd look at the php manual to see how to create an array format that works for you. The syntax I see mostly (and use) is: $colors[] = "#000000"; $colors[] = "#000001"; Or $colors = array(#000000,#000001,......); These elements would then be referenced with with a foreach as you did or with a for command as in: $maxclrs = count($colors); for($i=0; $i<$maxclrs; $i++) { do something referencing $colors[$i] } Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530251 Share on other sites More sharing options...
Gregan Posted January 22, 2016 Author Share Posted January 22, 2016 I'm trying to take a bunch of color hex codes and then sort them into an order that makes sense in a color picker tool. The code I have above is all the colors I'm looking to sort, then it changes the hex value into RGB, then to HSL color codes that are then sorted. It apparently looks great once it executes properly. I'll try your syntax.... Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530252 Share on other sites More sharing options...
ginerjm Posted January 22, 2016 Share Posted January 22, 2016 I tried this once. It ain't that easy! Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530253 Share on other sites More sharing options...
Gregan Posted January 22, 2016 Author Share Posted January 22, 2016 Try doing it when you don't code that often. Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530254 Share on other sites More sharing options...
QuickOldCar Posted January 23, 2016 Share Posted January 23, 2016 (edited) Somewhere in my old files I did this. I created arrays with range() using 0-255 rgb values for every combination (16,777,216 possible color values), then conversions for 3 and 6 digit hex. You can create this and save as an associative array, even better a database. So your server doesn't puke...probably better off making 3 variables and cycle in chunks as groups to save into a database. Like 0,0,0 to 255,255,255 Edited January 23, 2016 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530258 Share on other sites More sharing options...
Barand Posted January 23, 2016 Share Posted January 23, 2016 this will convert your existing $colors array to a simple indexed array ($newcolors) $newcolors = array(); foreach ($colors as $color) { $newcolors[] = str_replace(' ', '', $color['color']); } Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530260 Share on other sites More sharing options...
QuickOldCar Posted January 23, 2016 Share Posted January 23, 2016 (edited) Here is something that can actually store them, I looped just 10,000 of them. I commented out the section for your loop and checking hue ranges. <?php /** * Convert a hexadecimal color in RGB * @param string $hex * @return array */ function hexToHsl($hex){ list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x"); return rgbToHsl($r, $g, $b); } /** * Convert a RGB color in its HSL value * @param int $r red * @param int $g green * @param int $b blue * @return array */ function rgbToHsl($r, $g, $b) { $r /= 255; $g /= 255; $b /= 255; $max = max($r, $g, $b); $min = min($r, $g, $b); $h = 0; $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('h' => round($h, 2), 's' => round($s, 2), 'l' => round($l, 2)); } /* foreach ($colors as &$color) { $color['hsl'] = hexToHsl($color['color']); } usort($colors, function ($a, $b) { //Compare the hue when they are in the same "range" if(!huesAreinSameInterval($a['hsl']['h'],$b['hsl']['h'])){ if ($a['hsl']['h'] < $b['hsl']['h']) return -1; if ($a['hsl']['h'] > $b['hsl']['h']) return 1; } if ($a['hsl']['l'] < $b['hsl']['l']) return 1; if ($a['hsl']['l'] > $b['hsl']['l']) return -1; if ($a['hsl']['s'] < $b['hsl']['s']) return -1; if ($a['hsl']['s'] > $b['hsl']['s']) return 1; return 0; }); */ /** * Check if two hues are in the same given interval * @param float $hue1 * @param float $hue2 * @param int $interval * @return bool */ function huesAreinSameInterval($hue1, $hue2, $interval = 30){ return (round(($hue1 / $interval), 0, PHP_ROUND_HALF_DOWN) === round(($hue2 / $interval), 0, PHP_ROUND_HALF_DOWN)); } function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') { $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string $rgbArray = array(); if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster $colorVal = hexdec($hexStr); $rgbArray['red'] = 0xFF & ($colorVal >> 0x10); $rgbArray['green'] = 0xFF & ($colorVal >> 0x8); $rgbArray['blue'] = 0xFF & $colorVal; } elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2)); $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2)); $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2)); } else { return false; //Invalid hex color code } return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array } //hex2RGB("#FF0") -> array( red =>255, green => 255, blue => 0) //hex2RGB("#FFFF00) -> Same as above //hex2RGB("#FF0", true) -> 255,255,0 //hex2RGB("#FF0", true, ":") -> 255:255:0 $array = array(); //16777215 for all combinations for($i = 0; $i <= 10000; $i++) { $hex = sprintf('%06s', dechex($i)); $rgb = hex2RGB("#$hex", true); $hsl = hexToHsl("#$hex"); $rgb_colors = explode(",",$rgb); $array[] = array("rgb"=>$rgb,"rgbarray"=>array("r"=>$rgb_colors[0],"g"=>$rgb_colors[1],"b"=>$rgb_colors[2]),"hex"=>$hex,"hsl"=>$hsl); } echo "<pre>"; print_r($array); echo "</pre>"; ?> Returns data as so... Array( [0] => Array ( [rgb] => 0,0,0 [rgbarray] => Array ( [r] => 0 [g] => 0 => 0 ) [hex] => 000000 [hsl] => Array ( [h] => 0 => 0 [l] => 0 ) ) [1] => Array ( [rgb] => 0,0,1 [rgbarray] => Array ( [r] => 0 [g] => 0 => 1 ) [hex] => 000001 [hsl] => Array ( [h] => 240 => 1 [l] => 0 ) ) [2] => Array ( [rgb] => 0,0,2 [rgbarray] => Array ( [r] => 0 [g] => 0 => 2 ) [hex] => 000002 [hsl] => Array ( [h] => 240 => 1 [l] => 0 ) ) [3] => Array ( [rgb] => 0,0,3 [rgbarray] => Array ( [r] => 0 [g] => 0 => 3 ) [hex] => 000003 [hsl] => Array ( [h] => 240 => 1 [l] => 0.01 ) ) [4] => Array ( [rgb] => 0,0,4 [rgbarray] => Array ( [r] => 0 [g] => 0 => 4 ) [hex] => 000004 [hsl] => Array ( [h] => 240 => 1 [l] => 0.01 ) ) Edited January 23, 2016 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530263 Share on other sites More sharing options...
Barand Posted January 23, 2016 Share Posted January 23, 2016 Disregard my last post, I understand your data array structure now. This "works" but I don't know what results you are expecting. <?php $colors = [ [ 'color'=> '# 000000 '], [ 'color'=> '# 97233F '], [ 'color'=> '# 841F27 '], [ 'color'=> '# A71930 '], [ 'color'=> '# EF0107 '], [ 'color'=> '# 94BEE5 '], [ 'color'=> '# CE1141 '], [ 'color'=> '# E13A3E '], [ 'color'=> '# DF4601 '], [ 'color'=> '# 241773 '], [ 'color'=> '# 008348 '], [ 'color'=> '# BD3039 '], [ 'color'=> '# 061922 '], [ 'color'=> '# 00338D '], [ 'color'=> '# 002E62 '], [ 'color'=> '# 8CCCE5 '], [ 'color'=> '# E03A3E '], [ 'color'=> '# 0085CA '], [ 'color'=> '# 1D1160 '], [ 'color'=> '# 034694 '], [ 'color'=> '# 0B162A '], [ 'color'=> '# E3263A '], [ 'color'=> '# CC3433 '], [ 'color'=> '# AF2626 '], [ 'color'=> '# AA002C '], [ 'color'=> '# C6011F '], [ 'color'=> '# FB4F14 '], [ 'color'=> '# 860038 '], [ 'color'=> '# E31937 '], [ 'color'=> '# 8B2942 '], [ 'color'=> '# 91022D '], [ 'color'=> '# 333366 '], [ 'color'=> '# 00285C '], [ 'color'=> '# 1B458F '], [ 'color'=> '# 002244 '], [ 'color'=> '# 007DC5 '], [ 'color'=> '# 006A4E '], [ 'color'=> '# 4D90CD '], [ 'color'=> '# 005A8B '], [ 'color'=> '# ED174C '], [ 'color'=> '# EC1F26 '], [ 'color'=> '# 0C2C56 '], [ 'color'=> '# 003777 '], [ 'color'=> '# 274488 '], [ 'color'=> '# CF0032 '], [ 'color'=> '# C8213F '], [ 'color'=> '# FDB927 '], [ 'color'=> '# 203731 '], [ 'color'=> '# 002D62 '], [ 'color'=> '# F36600 '], [ 'color'=> '# 03202F '], [ 'color'=> '# F5A12D '], [ 'color'=> '# FFC633 '], [ 'color'=> '# 002C5F '], [ 'color'=> '# E31837 '], [ 'color'=> '# 004687 '], [ 'color'=> '# FDBE11 '], [ 'color'=> '# 00A398 '], [ 'color'=> '# BA0021 '], [ 'color'=> '# EF3E42 '], [ 'color'=> '# 00245D '], [ 'color'=> '# 5CBFEB '], [ 'color'=> '# DA020E '], [ 'color'=> '# 0F586C '], [ 'color'=> '# 008E97 '], [ 'color'=> '# 98002E '], [ 'color'=> '# FF6600 '], [ 'color'=> '# 0A2351 '], [ 'color'=> '# 00471B '], [ 'color'=> '# 005083 '], [ 'color'=> '# 002B5C '], [ 'color'=> '# 4F2683 '], [ 'color'=> '# 025736 '], [ 'color'=> '# BF2F38 '], [ 'color'=> '# 122089 '], [ 'color'=> '# FDBB2F '], [ 'color'=> '# 0A2141 '], [ 'color'=> '# 9F8958 '], [ 'color'=> '# 6CADDF '], [ 'color'=> '# 0B2265 '], [ 'color'=> '# 00529B '], [ 'color'=> '# 006BB6 '], [ 'color'=> '# FF5910 '], [ 'color'=> '# 0161AB '], [ 'color'=> '# D50031 '], [ 'color'=> '# E4002B '], [ 'color'=> '# 00B6F1 '], [ 'color'=> '# 003831 '], [ 'color'=> '# A5ACAF '], [ 'color'=> '# 007DC3 '], [ 'color'=> '# 633492 '], [ 'color'=> '# E4173E '], [ 'color'=> '# 004953 '], [ 'color'=> '# F47940 '], [ 'color'=> '# 284898 '], [ 'color'=> '# 001B2D '], [ 'color'=> '# E56020 '], [ 'color'=> '# FDB827 '], [ 'color'=> '# 004812 '], [ 'color'=> '# 005CAB '], [ 'color'=> '# A50531 '], [ 'color'=> '# 724C9F '], [ 'color'=> '# BAC3C9 '], [ 'color'=> '# 05143F '], [ 'color'=> '# AA0000 '], [ 'color'=> '# FD5A1E '], [ 'color'=> '# 0051BA '], [ 'color'=> '# 05535D '], [ 'color'=> '# 4F8A10 '], [ 'color'=> '# ED1A3B '], [ 'color'=> '# 91B0D5 '], [ 'color'=> '# 0546A0 '], [ 'color'=> '# C41E3A '], [ 'color'=> '# EB172B '], [ 'color'=> '# D50A0A '], [ 'color'=> '# 013E7D '], [ 'color'=> '# 092C5C '], [ 'color'=> '# C0111F '], [ 'color'=> '# 134A8E '], [ 'color'=> '# D80016 '], [ 'color'=> '# 001C58 '], [ 'color'=> '# 07346F '], [ 'color'=> '# 12264C '], [ 'color'=> '# CF132B '], [ 'color'=> '# AB0003 '], [ 'color'=> '# 773141 '], [ 'color'=> '# 091453 '], [ 'color'=> '# 60223B '], [ 'color'=> '# 91764B '], [ 'color'=> '# 023474 '], [ 'color'=> '# 7A003C '], [ 'color'=> '# 13274F '], [ 'color'=> '# C4D600 '], [ 'color'=> '# FFC422 '], [ 'color'=> '# BB9753 '], [ 'color'=> '# 0D2B56 '], [ 'color'=> '# E03A3F '], [ 'color'=> '# 565A5C '], [ 'color'=> '# B95915 '], [ 'color'=> '# B1BABF '], [ 'color'=> '# C60C30 '], [ 'color'=> '# FAB383 '], [ 'color'=> '# A9B0B8 '], [ 'color'=> '# 313F49 '], [ 'color'=> '# 20385B '], [ 'color'=> '# D7A22A '], [ 'color'=> '# BEC0C2 '], [ 'color'=> '# FFD200 '], [ 'color'=> '# 8177B7 '], [ 'color'=> '# BCBEC0 '], [ 'color'=> '# FFE600 '], [ 'color'=> '# EEE3C7 '], [ 'color'=> '# 231F20 '], [ 'color'=> '# 8A8D8F '], [ 'color'=> '# F0B83D '], [ 'color'=> '# C4CED3 '], [ 'color'=> '# F9A01B '], [ 'color'=> '# 00539B '], [ 'color'=> '# FF7900 '], [ 'color'=> '# B4975A '], [ 'color'=> '# 5B2B2F '], [ 'color'=> '# 006778 '], [ 'color'=> '# C09A5B '], [ 'color'=> '# 0053A0 '], [ 'color'=> '# D00027 '], [ 'color'=> '# 003263 '], [ 'color'=> '# 005A9C '], [ 'color'=> '# 004689 '], [ 'color'=> '# AFB7BA '], [ 'color'=> '# 552582 '], [ 'color'=> '# FFCE65 '], [ 'color'=> '# FFE500 '], [ 'color'=> '# 7399C6 '], [ 'color'=> '# F58220 '], [ 'color'=> '# 0077C8 '], [ 'color'=> '# B6922E '], [ 'color'=> '# F0EBD2 '], [ 'color'=> '# 00A94F '], [ 'color'=> '# D31145 '], [ 'color'=> '# FFC62F '], [ 'color'=> '# BF2B37 '], [ 'color'=> '# 213770 '], [ 'color'=> '# 00285E '], [ 'color'=> '# F57D31 '], [ 'color'=> '# F58426 '], [ 'color'=> '# 002D72 '], [ 'color'=> '# E6393F '], [ 'color'=> '# 012055 '], [ 'color'=> '# 003087 '], [ 'color'=> '# BBBDBF '], [ 'color'=> '# EFB21E '], [ 'color'=> '# F05133 '], [ 'color'=> '# FDE192 '], [ 'color'=> '# E81828 '], [ 'color'=> '# B18500 '], [ 'color'=> '# D1BD80 '], [ 'color'=> '# EBE72B '], [ 'color'=> '# 013474 '], [ 'color'=> '# 8E9090 '], [ 'color'=> '# 0073CF '], [ 'color'=> '# B3995D '], [ 'color'=> '# F38F20 '], [ 'color'=> '# 005C5C '], [ 'color'=> '# 69BE28 '], [ 'color'=> '# 11568C '], [ 'color'=> '# 211E1F '], [ 'color'=> '# FFC325 '], [ 'color'=> '# 000066 '], [ 'color'=> '# 1B449C '], [ 'color'=> '# A68A26 '], [ 'color'=> '# 34302B '], [ 'color'=> '# 8FBCE6 '], [ 'color'=> '# 4B92DB '], [ 'color'=> '# 003278 '], [ 'color'=> '# 1D2D5C '], [ 'color'=> '# 047A4A '], [ 'color'=> '# 00214E '], [ 'color'=> '# 11225B '], [ 'color'=> '# F7C240 '], [ 'color'=> '# 0168AB '], [ 'color'=> '# EF5225 '], [ 'color'=> '# FFB612 '], [ 'color'=> '# EFE1C6 '], [ 'color'=> '# E3D4AD '], [ 'color'=> '# 9C824A '], ]; foreach ($colors as $k => $color) { $colors[$k]['color'] = str_replace(' ', '', $color['color']); $colors[$k]['hsl']= hexToHsl($color['color']); } // // now sort the colors array by HSL value // usort($colors, function ($a, $b) { $dh = 100*($a['hsl']['h']) - 100*($b['hsl']['h']); if ($dh==0) { $ds = 100*($a['hsl']['s']) - 100*($b['hsl']['s']); if ($ds==0) { return 100*($a['hsl']['l']) - 100*($b['hsl']['l']); } else return $ds; } else return $dh; }); /** * Convert a hexadecimal color in RGB * @param string $hex * @return array */ function hexToHsl($hex){ list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x"); return rgbToHsl($r, $g, $b); } /** * Convert a RGB color in its HSL value * @param int $r red * @param int $g green * @param int $b blue * @return array */ function rgbToHsl($r, $g, $b) { $r /= 255; $g /= 255; $b /= 255; $max = max($r, $g, $b); $min = min($r, $g, $b); $h = 0; $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('h' => round($h, 2), 's' => round($s, 2), 'l' => round($l, 2)); } /** * Check if two hues are in the same given interval * @param float $hue1 * @param float $hue2 * @param int $interval * @return bool */ function huesAreinSameInterval($hue1, $hue2, $interval = 30){ return (round(($hue1 / $interval), 0, PHP_ROUND_HALF_DOWN) === round(($hue2 / $interval), 0, PHP_ROUND_HALF_DOWN)); } ?> <html> <head> <title>Colors</title> <style type="text/css"> .clr { width: 20px; height: 20px; float: left; } </style> </head> <body> <?php foreach ($colors as $k=>$col) { $c = $col['color']; echo "<div class='clr' style='background-color: $c'> </div>\n"; if ( ($k+1)%15==0) echo "<div style='clear:left'></div>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530264 Share on other sites More sharing options...
Gregan Posted January 23, 2016 Author Share Posted January 23, 2016 I really appreciate you getting it to work and spit out a result, the output color arrangement isn't nearly as cool/clean as I had anticipated based on the original author's comments when he shared the code. Looks like I need to step away from this problem as it's well above my current abilities, I need to pass this onto someone else to solve when I find someone to code my actual website. Thanks again for your time and help. Quote Link to comment https://forums.phpfreaks.com/topic/300625-brand-new-to-php-and-i-havent-coded-in-a-few-years/#findComment-1530318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.