Results 1 to 6 of 6
-
05-27-2010, 12:49 PM #1Newbie
- Join Date
- Nov 2002
- Posts
- 8
PHP Challenge: Sorting by HTML color codes. $15 for solution
What I have in my database is a table containing almost 25,000 rows. Each row has the following fields:
imageID | htmlColorCode | colorCount | base
imageID is an id. (INT)
htmlColorCode is an HTML hex code. (VARCHAR)
colorCount is a count of the color in the image. (INT)
base is the html hex code converted from base 16 to base 10. (INT)
I run a query like this: SELECT * FROM table GROUP BY htmlColorCode ORDER BY base DESC
That results in a table of colors like this. As you can see, the colors are ordered roughly from light to dark.
The challenge is to group colors together. All greens should be together, red together, etc etc...
Maybe you can group the colors together with a simple query or have some other clever solution?
The first person to provide a solution or offer a suggestion that leads me to the solution will get the $15. It will be sent by PayPal.
-
05-27-2010, 03:31 PM #2Junior Guru Wannabe
- Join Date
- Mar 2006
- Location
- UK
- Posts
- 38
As you store the colours into the database covert them to HLS now if memory serves me you should be able select and order by hue, lightness and saturation. The hue should keep the greens together, reds together and so on.
You should be able to get away with just storing the int value for the RGB colour rather than the hex string as you can covert it back to a string quite easily.
The conversion from RGB to HLS should be easy enough to find on the net if you don't have it, if you can't find it let me know and I'll look for my books.
-
05-27-2010, 03:55 PM #3Disabled
- Join Date
- Oct 2005
- Location
- Six Degrees From You
- Posts
- 1,079
Order by htmlColorCode rater than base.
-
05-27-2010, 04:00 PM #4Web Hosting Master
- Join Date
- Feb 2006
- Location
- Kusadasi, Turkey
- Posts
- 3,379
Here is your RGB to HSL converter code, modified from:
http://stackoverflow.com/questions/1...php-gd-library
PHP Code:function rgb2hsl($r, $g, $b) {
$var_R = ($r / 255);
$var_G = ($g / 255);
$var_B = ($b / 255);
$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;
$v = $var_Max;
if ($del_Max == 0) {
$h = 0;
$s = 0;
} else {
$s = $del_Max / $var_Max;
$del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
if ($var_R == $var_Max) $h = $del_B - $del_G;
else if ($var_G == $var_Max) $h = ( 1 / 3 ) + $del_R - $del_B;
else if ($var_B == $var_Max) $h = ( 2 / 3 ) + $del_G - $del_R;
if ($h < 0) $h++;
if ($h > 1) $h--;
}
return array($h, $s, $v);
}
function hsl2rgb($h, $s, $v) {
if($s == 0) {
$r = $g = $B = $v * 255;
} else {
$var_H = $h * 6;
$var_i = floor( $var_H );
$var_1 = $v * ( 1 - $s );
$var_2 = $v * ( 1 - $s * ( $var_H - $var_i ) );
$var_3 = $v * ( 1 - $s * (1 - ( $var_H - $var_i ) ) );
if ($var_i == 0) { $var_R = $v ; $var_G = $var_3 ; $var_B = $var_1 ; }
else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $v ; $var_B = $var_1 ; }
else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $v ; $var_B = $var_3 ; }
else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $v ; }
else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $v ; }
else { $var_R = $v ; $var_G = $var_1 ; $var_B = $var_2 ; }
$r = $var_R * 255;
$g = $var_G * 255;
$B = $var_B * 255;
}
return array($r, $g, $B);
}
H: Hue, means red, green, blue, etc. Sort by it and you get closer colors together.
S: Saturation, means grayness.
L: Brightness (lightness?), you can sort by it to have light to dark sort.
However note that colors have three dimensions, and you can't sort them in two dimensions, both being grouped in similar colors and sorted from light to dark.
Here is an example question: Which one comes after "light green", dark green or light cyan? One is a better green, one has a closer lightness. So you'll probably get stuck in any case█ Fraud Record - Stop Fraud Clients, Report Abusive Customers.
█ Combine your efforts to fight misbehaving clients.
█ HarzemDesign - Highest quality, well designed and carefully coded hosting designs. Not cheap though.
█ Large and awesome portfolio, just visit and see!
-
05-28-2010, 07:40 PM #5Newbie
- Join Date
- Nov 2002
- Posts
- 8
Thanks to those of you who replied. I'm currently testing out the methods presented here as well as those submitted by pm and aim. I'll post again with the results once my testing is complete.
-
05-30-2010, 08:55 PM #6Newbie
- Join Date
- Nov 2002
- Posts
- 8
Thanks again to those of you who offered suggestions. Reeces here on the forum provided the solution that most closely fits my needs. The results can be seen in my new color table. I've sought help from Reece a couple of times before. He's a very bright guy and quite tenacious!
Similar Threads
-
I need PHP code for search by HTML HEX codes.
By jeremed in forum Employment / Job OffersReplies: 1Last Post: 05-17-2010, 09:39 PM -
Ethernet Color Codes
By WinApp in forum Hosting Security and TechnologyReplies: 8Last Post: 05-10-2004, 10:44 AM -
HTML Table challenge...
By nocturnix123 in forum Web Design and ContentReplies: 4Last Post: 10-02-2003, 09:05 PM -
Anyone want an HTML challenge? Need help bad :(
By shibby in forum Web Hosting LoungeReplies: 5Last Post: 05-14-2003, 09:58 AM