This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
923 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the DBStatisticBundle package. | ||
* | ||
* (c) FOUQUET <https://github.com/hugo082/DBStatisticBundle> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Hugo Fouquet <[email protected]> | ||
*/ | ||
|
||
namespace DB\StatisticBundle\Core; | ||
|
||
use Symfony\Component\Config\Definition\Exception\Exception; | ||
|
||
class Color | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $red; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $green; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $blue; | ||
|
||
/** | ||
* @var float | ||
*/ | ||
private $alpha; | ||
|
||
public function __construct(int $red, int $green, int $blue, float $alpha) | ||
{ | ||
$this->red = $red; | ||
$this->green = $green; | ||
$this->blue = $blue; | ||
$this->alpha = $alpha; | ||
} | ||
|
||
/** | ||
* Returns whether or not given color is considered "light" | ||
* @param int $lighterThan | ||
* @return bool | ||
* @internal param bool|string $color | ||
*/ | ||
public function isLight(int $lighterThan = 130){ | ||
return $this->getLuma() > $lighterThan; | ||
} | ||
|
||
/** | ||
* Returns whether or not given color is considered "dark" | ||
* @param int $lighterThan | ||
* @return bool | ||
* @internal param bool|string $color | ||
*/ | ||
public function isDark(int $lighterThan = 130){ | ||
return $this->getLuma() <= $lighterThan; | ||
} | ||
|
||
/** | ||
* Darkens component | ||
* @param float $delta | ||
* @return Color | ||
*/ | ||
public function darken(float $delta = 0.7): Color { | ||
$this->red *= $delta; | ||
$this->green *= $delta; | ||
$this->blue *= $delta; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Lightens component | ||
* @param float $delta | ||
* @return Color | ||
*/ | ||
public function lighten(float $delta = 0.7): Color { | ||
$this->red += (255 - $this->red) * $delta; | ||
$this->green += (255 - $this->green) * $delta; | ||
$this->blue += (255 - $this->blue) * $delta; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param Color $color | ||
* @param int $delta | ||
* @param float|null $alpha | ||
* @return Color | ||
*/ | ||
public static function randomFrom(Color $color, int $delta = 20, float $alpha = null): Color { | ||
$alpha = ($alpha) ? $alpha : $color->getAlpha(); | ||
return new Color(self::rdmDelta($color->getRed(), $delta), self::rdmDelta($color->getGreen(), $delta), | ||
self::rdmDelta($color->getBlue(), $delta), $alpha); | ||
} | ||
|
||
/** | ||
* @param float|null $alpha | ||
* @return Color | ||
*/ | ||
public static function random(float $alpha = null): Color { | ||
$alpha = ($alpha) ? $alpha : mt_rand(0, 255) / 255; | ||
return new Color(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), $alpha); | ||
} | ||
|
||
/** | ||
* @param float|null $alpha | ||
* @return Color | ||
*/ | ||
public static function randomDarken(float $alpha = null, int $luma = 80): Color { | ||
$color = self::random(); | ||
while (!$color->isDark($luma)) | ||
$color->darken(0.9); | ||
return $color; | ||
} | ||
|
||
/** | ||
* @param Color $color | ||
* @param float $delta | ||
* @return Color | ||
*/ | ||
public static function darkenFrom(Color $color, float $delta = 0.7): Color { | ||
$color = self::dublicate($color); | ||
return $color->darken($delta); | ||
} | ||
|
||
/** | ||
* @param Color $color | ||
* @param float $delta | ||
* @return Color | ||
*/ | ||
public static function lightenFrom(Color $color, float $delta = 0.7): Color { | ||
$color = self::dublicate($color); | ||
return $color->lighten($delta); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getRed(): int | ||
{ | ||
return $this->red; | ||
} | ||
|
||
/** | ||
* @param int $red | ||
*/ | ||
public function setRed(int $red) | ||
{ | ||
$this->red = $red; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getGreen(): int | ||
{ | ||
return $this->green; | ||
} | ||
|
||
/** | ||
* @param int $green | ||
*/ | ||
public function setGreen(int $green) | ||
{ | ||
$this->green = $green; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getBlue(): int | ||
{ | ||
return $this->blue; | ||
} | ||
|
||
/** | ||
* @param int $blue | ||
*/ | ||
public function setBlue(int $blue) | ||
{ | ||
$this->blue = $blue; | ||
} | ||
|
||
/** | ||
* @return float | ||
*/ | ||
public function getAlpha(): float | ||
{ | ||
return $this->alpha; | ||
} | ||
|
||
/** | ||
* @param float $alpha | ||
*/ | ||
public function setAlpha(float $alpha) | ||
{ | ||
$this->alpha = $alpha; | ||
} | ||
|
||
/** | ||
* @return Int | ||
*/ | ||
public function getLuma(): Int { | ||
return ( $this->red * 299 + $this->green * 587 + $this->blue * 114 ) / 1000; | ||
} | ||
|
||
public function getRGBA(): string { | ||
return 'rgba(' . round($this->red) . ',' . round($this->blue) . ',' . round($this->green) . ',' . round($this->alpha, 3) . ')'; | ||
} | ||
|
||
/** | ||
* Random int between bounds | ||
* @param int $min | ||
* @param int $max | ||
* @param int $cutMin | ||
* @param int $cutMax | ||
* @return int | ||
*/ | ||
private static function rdmBtw(int $min, int $max, int $cutMin = 0, int $cutMax = 255): int { | ||
return mt_rand(max($min, $cutMin), min($max, $cutMax)); | ||
} | ||
|
||
/** | ||
* @param int $value | ||
* @param int $delta | ||
* @param int $min | ||
* @param int $max | ||
* @return int | ||
*/ | ||
private static function rdmDelta(int $value, int $delta, int $min = 0, int $max = 255): int { | ||
return self::rdmBtw($value - $delta, $value + $delta, $min, $max); | ||
} | ||
|
||
/** | ||
* @param Color $color | ||
* @return Color | ||
*/ | ||
private static function dublicate(Color $color): Color { | ||
return new Color($color->red, $color->blue, $color->green, $color->alpha); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the DBStatisticBundle package. | ||
* | ||
* (c) FOUQUET <https://github.com/hugo082/DBStatisticBundle> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Hugo Fouquet <[email protected]> | ||
*/ | ||
|
||
namespace DB\StatisticBundle\Core; | ||
|
||
use Symfony\Component\Config\Definition\Exception\Exception; | ||
|
||
class Data | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $lines; | ||
|
||
public function __construct() | ||
{ | ||
$this->lines = array(); | ||
} | ||
|
||
public function encode(): array { | ||
$lines = array(); | ||
$labels = array(); | ||
/** @var Line $item */ | ||
foreach ($this->lines as $line) { | ||
$encode = $line->encode(); | ||
$labels = $encode["labels"]; | ||
$lines[] = $encode; | ||
} | ||
return array( | ||
"datasets" => $lines, | ||
"labels" => $labels | ||
); | ||
} | ||
|
||
public function sortItems(callable $cmp) { | ||
/** @var Line $line */ | ||
foreach ($this->lines as $line) | ||
$line->sortItems($cmp); | ||
} | ||
|
||
public function sortItemsByDate(string $format) { | ||
/** @var Line $line */ | ||
foreach ($this->lines as $line) | ||
$line->sortItemsByDate($format); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param string|null $label | ||
* @return Line | ||
* @throws \Exception | ||
*/ | ||
public function createLine(string $id, string $label = null) { | ||
if (key_exists($id, $this->lines)) | ||
throw new \Exception("Line with id '" . $id . "' already exist."); | ||
$this->lines[$id] = new Line($id, $label); | ||
return $this->lines[$id]; | ||
} | ||
|
||
/** | ||
* @param string $label | ||
* @param float $value | ||
* @param string|null $lineID | ||
* @throws \Exception | ||
*/ | ||
public function incrementValueForItemWithLabel(string $label, float $value, string $lineID = null) { | ||
$line = $this->getLineWithID($lineID); | ||
if ($line == null) | ||
throw new \Exception("Impossible to increment value of item with label '" . $label . "'. Line '" . $lineID . "' doesn't exist."); | ||
$line->incrementValueForItemWithLabel($label, $value); | ||
} | ||
|
||
/** | ||
* @param string|null $id | ||
* @return Line|null | ||
*/ | ||
public function getLineWithID(string $id = null) { | ||
if ($id == null && !empty($this->lines)) | ||
return array_values($this->lines)[0]; | ||
if (key_exists($id, $this->lines)) | ||
return $this->lines[$id]; | ||
return null; | ||
} | ||
} |
Oops, something went wrong.