Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/209828-create-colors-based-on-of-items/
Share on other sites

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 ;)

 

 

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 />";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.