Skip to content

Color #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions CSSParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
require_once 'lib/ColorUtils.php';
require_once('lib/CSSProperties.php');
require_once('lib/CSSList.php');
require_once('lib/CSSRuleSet.php');
Expand Down Expand Up @@ -98,16 +99,26 @@ private function parseAtRule() {
}
}

private function parseIdentifier($bAllowFunctions = true) {
private function parseIdentifier($bAllowFunctions = false, $bAllowColors = false) {
$sResult = $this->parseCharacter(true);
if($sResult === null) {
throw new Exception("Identifier expected, got {$this->peek(5)}");
}
$sCharacter;
while(($sCharacter = $this->parseCharacter(true)) !== null) {
$sResult .= $sCharacter;
}
if($bAllowFunctions && $this->comes('(')) {
}
if($bAllowColors)
{
// is it a color name ?
if($aColor = ColorUtils::namedColor2rgb($sResult))
{
$oColor = new CSSColor();
return $oColor->fromRGB($aColor);
}
}
if($bAllowFunctions && $this->comes('('))
{
$this->consume('(');
$sResult = new CSSFunction($sResult, $this->parseValue());
$this->consume(')');
Expand Down Expand Up @@ -252,15 +263,15 @@ private function parseSingleValue() {
} else if($this->comes("'") || $this->comes('"')){
$oValue = $this->parseStringValue();
} else {
$oValue = $this->parseIdentifier();
$oValue = $this->parseIdentifier(true, true);
}
$this->consumeWhiteSpace();
if($this->comes('/')) {
$this->consume('/');
$oValue = new CSSSlashedValue($oValue, $this->parseSingleValue());
}
return $oValue;
}
}

private function parseNumericValue($bForColor = false) {
$sSize = '';
Expand Down Expand Up @@ -305,19 +316,16 @@ private function parseNumericValue($bForColor = false) {
}

private function parseColorValue() {
$aColor = array();
if($this->comes('#')) {
$this->consume('#');
$sValue = $this->parseIdentifier(false);
if(mb_strlen($sValue, $this->sCharset) === 3) {
$sValue = $sValue[0].$sValue[0].$sValue[1].$sValue[1].$sValue[2].$sValue[2];
}
$aColor = array('r' => new CSSSize(intval($sValue[0].$sValue[1], 16), null, true), 'g' => new CSSSize(intval($sValue[2].$sValue[3], 16), null, true), 'b' => new CSSSize(intval($sValue[4].$sValue[5], 16), null, true));
$sValue = $this->parseIdentifier();
return new CSSColor($sValue);
} else {
$sColorMode = $this->parseIdentifier(false);
$aColor = array();
$sColorMode = $this->parseIdentifier();
$this->consumeWhiteSpace();
$this->consume('(');
$iLength = mb_strlen($sColorMode, $this->sCharset);
$iLength = strlen($sColorMode);
for($i=0;$i<$iLength;$i++) {
$this->consumeWhiteSpace();
$aColor[$sColorMode[$i]] = $this->parseNumericValue(true);
Expand All @@ -327,8 +335,8 @@ private function parseColorValue() {
}
}
$this->consume(')');
return new CSSColor($aColor);
}
return new CSSColor($aColor);
}

private function parseURLValue() {
Expand Down
156 changes: 152 additions & 4 deletions lib/CSSValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function getName() {
return $this->sName;
}

public function setName($sName) {
$this->sName = $sName;
}

public function getArguments() {
return $this->aComponents;
}
Expand All @@ -58,17 +62,161 @@ public function __toString() {
}

class CSSColor extends CSSFunction {
public function __construct($aColor) {
parent::__construct(implode('', array_keys($aColor)), $aColor);

public function __construct($mColor=null) {
parent::__construct('rgb', null);
if(is_array($mColor)) {
if(isset($mColor['r'], $mColor['g'], $mColor['b'])) {
$this->fromRGB($mColor);
}
else if(isset($mColor['h'], $mColor['s'], $mColor['l'])) {
$this->fromHSL($mColor);
}
}
else if(is_string($mColor)) {
if($aRGB = ColorUtils::namedColor2rgb($mColor)) {
$this->fromRGB($aRGB);
}
else if($aRGB = ColorUtils::hex2rgb($mColor)) {
$this->fromRGB($aRGB);
}
}
}


public function fromRGB(Array $aRGB) {
$this->aComponents = array();
$sName = 'rgb';
foreach(array('r', 'g', 'b', 'a') as $sChannel) {
if($sChannel == 'a') {
if(!isset($aRGB['a'])) continue;
$sValue = ColorUtils::constrainValue((string)$aRGB['a'], 0, 1);
if($sValue == 1) continue;
$sName .= 'a';
}
else {
$sValue = ColorUtils::normalizeRGBValue((string)$aRGB[$sChannel]);
}
$this->aComponents[$sChannel] = new CSSSize($sValue, null, true);
}
$this->setName($sName);
return $this;
}

public function fromHSL(Array $aHSL) {
$aRGB = ColorUtils::hsl2rgb(
(string)$aHSL['h'],
(string)$aHSL['s'],
(string)$aHSL['l'],
isset($aHSL['a']) ? (string)$aHSL['a'] : 1
);
return $this->fromRGB($aRGB);
}

public function fromHex($sValue) {
$aRGB = ColorUtils::hex2rgb($sValue);
return $this->fromRGB($aRGB);
}

public function fromNamedColor($sValue) {
$aRGB = ColorUtils::namedColor2rgb($sValue);
return $this->fromRGB($aRGB);
}

public function getColor() {
return $this->aComponents;
}

public function getColorDescription() {
return $this->getName();
}
}

public function toRGB() {
$sName = $this->getName();
$aComponents = $this->aComponents;

if(!$sName || $sName == 'rgb') return;
if($sName == 'rgba') {
// If we don't need alpha channel, drop it
if($aComponents['a']->getSize() >= 1) {
unset($this->aComponents['a']);
$this->setName('rgb');
}
return;
}
$aRGB = ColorUtils::hsl2rgb(
$aComponents['h']->getSize(),
$aComponents['s']->getSize(),
$aComponents['l']->getSize(),
isset($aComponents['a']) ? $aComponents['a']->getSize() : 1
);

$this->aComponents = array();
foreach($aRGB as $key => $val) {
$this->aComponents[$key] = new CSSSize($val, null, true);
}
$sName = isset($aRGB['a']) ? 'rgba' : 'rgb';
$this->setName($sName);
return $this;
}

public function toHSL() {
$sName = $this->getName();
$aComponents = $this->aComponents;
if(!$sName || $sName == 'hsl') return;
if($sName == 'hsla') {
// If we don't need alpha channel, drop it
if($aComponents['a']->getSize() >= 1) {
unset($this->aComponents['a']);
$this->setName('hsl');
}
return;
}
$aHSL = ColorUtils::rgb2hsl(
$aComponents['r']->getSize(),
$aComponents['g']->getSize(),
$aComponents['b']->getSize(),
isset($aComponents['a']) ? $aComponents['a']->getSize() : 1
);
$this->aComponents = array();
$this->aComponents['h'] = new CSSSize($aHSL['h'], null, true);
$this->aComponents['s'] = new CSSSize($aHSL['s'], '%', true);
$this->aComponents['l'] = new CSSSize($aHSL['l'], '%', true);
$sName = 'hsl';
if(isset($aHSL['a'])) {
$this->aComponents['a'] = new CSSSize($aHSL['a'], null, true);
$sName = 'hsla';
}
$this->setName($sName);
return $this;
}

public function getNamedColor() {
$this->toRGB();
$aComponents = $this->aComponents;
return ColorUtils::rgb2NamedColor(
$aComponents['r']->getSize(),
$aComponents['g']->getSize(),
$aComponents['b']->getSize()
);
}

public function getHexValue() {
$aComponents = $this->aComponents;
if(isset($aComponents['a']) && $aComponents['a']->getSize() !== 1) return null;
$sName = $this->getName();
if($sName == 'rgb') {
return ColorUtils::rgb2hex(
$aComponents['r']->getSize(),
$aComponents['g']->getSize(),
$aComponents['b']->getSize()
);
}
else if($sName == 'hsl') {
return ColorUtils::hsl2hex(
$aComponents['h']->getSize(),
$aComponents['s']->getSize(),
$aComponents['l']->getSize()
);
}
}
}
Loading