nick1 Posted August 4, 2010 Share Posted August 4, 2010 Preferably using PHP, I'd like to create X number of colors based on X number of items and using a predefined color range. For example: ITEM VALUE 1 1,234,567,890 2 234,567,890 3 34,567,890 4 4,567,890 5 567,890 Color Range red = largest value green = medium value blue = smallest value Example Item 1 gets color red Item 2 gets whatever color naturally comes between red and green Item 3 gets color green Item 4 gets whatever color naturally comes between green and blue Item 5 gets color blue Requirements The code should be able to handle any number of items. 5 items was just an easy example. Some Thoughts - Number of items = number of colors - How to divide a color range into number of items? - Colors can be represented in decimal format: red=255,000,000 blue=000,000,255 - Can a formula be created, based on the color decimal format, to solve this problem? I think it's the math portion of the problem that I'm stuck on right now. Thanks for your help, Nick Quote Link to comment https://forums.phpfreaks.com/topic/209828-create-colors-based-on-of-items/ Share on other sites More sharing options...
phil88 Posted August 4, 2010 Share Posted August 4, 2010 You could decrease the red value by x and increase green by x for every item from the 1st to the middle item. Then you decrease the green value by x and increase the blue value by x for every item until you reach the last item. x will have the same value throughout because you can work it out based on the number of items. Here's some psuedocode to help explain what I mean; $numItems = count($items); $middleItem = $numItems / 2; $red = 255; $green = 0; $blue = 0; $x = ?; foreach($items as $index => $item){ if($index < $middleItem){ $red -= $x; $green += $x; }else{ // $red should be 0 by now if $x is set correctly // $green should be 255 $green -= $x; $blue += $x; } echo $item in the colour rbg($red, $green, $blue); } I left you to work out how you figure out what the increment/decrement ($x) should be Quote Link to comment https://forums.phpfreaks.com/topic/209828-create-colors-based-on-of-items/#findComment-1095307 Share on other sites More sharing options...
nick1 Posted August 10, 2010 Author Share Posted August 10, 2010 Thank you for your help! You showed me how to approach the problem from a different view - that's exactly the kind of help I needed. :-) Nick Quote Link to comment https://forums.phpfreaks.com/topic/209828-create-colors-based-on-of-items/#findComment-1097772 Share on other sites More sharing options...
jcbones Posted August 10, 2010 Share Posted August 10, 2010 Because this is an interesting proposal, I added a little code to the example above. It isn't perfect, as you can see if you un-comment the for loop, it kinda works. As was stated, getting $x is the big issue. <?php $items[] = 1234567890; $items[] = 234567890; $items[] = 34567890; $items[] = 4567890; $items[] = 567890; // for($i = 0; $i < 100; $i++) { // $items[] = mt_rand(); // } // rsort($items); $red = 255; $green = 0; $blue = 0; $middleNum = round((count($items) / 2) - 1); //Gets the middle index of the array. $x = floor(255 / $middleNum); foreach($items as $index => $item){ if($index > 0 && $index <= $middleNum){ $red -= $x; $green += $x; }elseif($index > $middleNum) { $green -= $x; $blue += $x; } echo number_format($item) . " is rgb($red,$green,$blue);<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209828-create-colors-based-on-of-items/#findComment-1097838 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.