<?php

/*  allow different things to have a distinct personality while conforming
 *  to a common set of rules
 *
 *  a problem that is encountered is that adding rules will need to erode the
 *  distinctiveness of the personality, often so much so that the personality
 *  is gone
 *
 *  to further complicate things, color while infinite in spectrum, only gives
 *  limited range when used to provide uniqueness, for example one might
 *  consider red and slightly different red the same color
 *
 *  we have several things that need to be tracked:
 *  1.  radon pCi/L 
 *  2.  temperature
 *  3.  humidity
 *  4.  pressure
 *  5.  air qualaity
 *
 *  in a home with ten sensors it is feasible that there be ten to fifty things
 *  to keep track of.  therefore the total number of things needing tracking
 *  is variable.
 *
 *  fitting more things into an enclosed space is obviously the job of a
 *  computer program, maintaining distinctiveness evenly, or even with a bias
 *  may also be the job of a computer program
 *
 *  an difficult to explain rule is that colors must converge, they still
 *  will need to be different, but if one picks starting colors, then it makes
 *  sense to say that as colors are generated based on the starting colors one
 *  would not want them to diverge, therefore, they must converge
 *
 *  for this example, I want the color to converge on red, but since I do not
 *  want everyone to be red, colors will instead converge into colors strictly
 *  between yellow and red, this limits starting colors somewhat more, but
 *  whatever
 *
 *  yellow is made when blue is removed from white
 *
 */
require_once("test-pattern.php");

// rule #1: each sensor is assigned a unique color
$LIST_OF_SENSORS = array(
    "cheese", 
    "chicken",
    "beef",
    "doornail",
    "toothrack"
);

// rule #2: sensor unique colors cannot contain quantities of reserved colors
$RESERVED_COLORS = array(
    "red"   => 127,
    "blue"  => 0,
    "green" => 0
);

// rule #3: tracked things have an importance, lower is more important
$LIST_OF_DATATYPES = array(
    "radon",
    "humidity",
    "pressure",
    "dust",
    "temperature"
);

$sensorCount = count($LIST_OF_SENSORS);
$datatypeCount = count($LIST_OF_DATATYPES);

header("Content-Type: application: xhtml+xml");
$test = new Paint();

// light gray for mixing (gray would be 0x808080)
$lightGray = new Pixel(0xD3, 0xD3, 0xD3);
// generate an gradient between red and yellow, use flags to make colors more distinct
$redToYellow = Pixel::generatePixelLine(
    new Pixel(/* red */ 255,0,0),
    new Pixel(/* yellow */ 255,255,0),
    array(/* options */
        "total" => $sensorCount,
        "flags" => PIXEL_ALTERNATE | PIXEL_ADD | PIXEL_BLUE,
        "bias" => 0.35
    )
);
$justGreen = new Pixel(/* green */ 0,255,0);
$someGreenThatILike = Pixel::mix($justGreen, new Pixel(/* blue */ 0,32,192));
// generate starting colors, I like green, use flags to make colors really distinct
$blueToGreen = Pixel::generatePixelLine(
    $someGreenThatILike,
    $justGreen,
    array(/* options */
        "total" => $sensorCount,
        "flags" => PIXEL_ALTERNATE | PIXEL_ADD | PIXEL_BLUE,
        "bias" => 0.5
    )
);


$radius = 20;
$cursor = new Coord(0,0);
// generate palette based on starting options
for($i=$sensorCount;$i--;) {
    // save the cursor's vertical position (technically we know it is zero, but whatever)
    $vertical = $cursor->y;
    // mix the starting and ending color in equal parts
    $mixture = Pixel::mix($blueToGreen[$i], $redToYellow[$i]);
    // mix this color with light gray with a 1:2 ratio
    $mixture = Pixel::mix($mixture, $lightGray, 1/3, 2/3);
    // for fun, make mixture fade into background
    $mixture->setAlpha(0.3);
    // but then have it fade back in, cool and fun!
    $redToYellow[$i]->setAlpha(0.65);
    // generate the first half of colors from start to this mixture, increase fade
    $startToMixture = Pixel::generatePixelLine(
        $blueToGreen[$i],
        $mixture,
        array(/* options */
            "total" => (int) round($sensorCount / 2, 0, PHP_ROUND_HALF_UP),
            "flags" => PIXEL_SUB | PIXEL_ALPHA,
            "bias" => /* increase rate of fade towards mixture */ 0.25
        )
    );
    // generate the second half of colors from this mixture to end
    $mixtureToEnd = Pixel::generatePixelLine(
        $mixture,
        $redToYellow[$i],
        array(/* options */
            "total" => (int) 1 + round($sensorCount / 2, 0, PHP_ROUND_HALF_DOWN)
        )
    );
    // last pixel in the first half is same as the first in second, remove it
    array_pop($startToMixture);
    // draw all the pretty colors for this sensor
    $mixed = array_merge($startToMixture, $mixtureToEnd);
    foreach($mixed as $pixel) {
        $test->addCircle(new Coord($cursor->x, $cursor->y), $pixel, $radius);
        // move the cursor down
        $cursor->y += ($radius * 2);
    }
    // move the cursor to the right
    $cursor->x += ($radius * 2);
    // restore the cursor's vertical position
    $cursor->y = $vertical;
}
echo $test;
