diff --git a/code/TypographyTestPage.php b/code/TypographyTestPage.php index ee7ff31..4d757a9 100644 --- a/code/TypographyTestPage.php +++ b/code/TypographyTestPage.php @@ -1,244 +1,264 @@ -get("TypographyTestPage", "css_folder")){ - $folder = Config::inst()->get("TypographyTestPage", "css_folder"); - } - else { - $folder = "themes/".SSViewer::current_theme()."/css/"; - } - $fullFolder = Director::baseFolder().'/'.$folder; - if(!file_exists($fullFolder)) { - user_error("could not find the default CSS folder $fullFolder"); - $folder = ''; - } - return $folder; - } - - private static $defaults = array( - 'URLSegment' => 'typo', - 'ShowInMenus' => false, - 'ShowInSearch' => false, - 'Title' => 'Typography Test', - 'Content' => 'auto-completed - do not alter', - 'ShowInMenus' => false, - 'ShowInSearch' => false, - 'Sort' => 99999 - ); - - - function canCreate($member = null) { - if(TypographyTestPage::get()->First()) { - return false; - } - return parent::canCreate($member); - } - - function requireDefaultRecords() { - if(self::$auto_include) { - $className = $this->class; - $page = $className::get()->First(); - if(! $page) { - $page = new TypographyTestPage(self::$defaults); - $parent = SiteTree::get_by_link(self::$parent_url_segment); - if($parent) { - $page->ParentID = $parent->ID; - } - $page->writeToStage('Stage'); - $page->publish('Stage', 'Live'); - $page->URLSegment = self::$defaults['URLSegment']; - $page->writeToStage('Stage'); - $page->publish('Stage', 'Live'); - DB::alteration_message('TypographyTestPage', 'created'); - } - } - } -} - -class TypographyTestPage_Controller extends Page_Controller { - - private static $allowed_actions = array( - "colours" => "ADMIN", - "replacecolours" => "ADMIN" - ); - - function init() { - parent::init(); - Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); - Requirements::javascript('typography/javascript/typography.js'); - } - - public function index() { - $this->Content = $this->typographyhtml(); - return array(); - } - - function ShowFirstHeading(){ - return Config::inst()->get("TypographyTestPage", "include_first_heading_in_test_copy"); - } - - public function colours(){ - $baseFolder = Director::baseFolder(); - require($baseFolder.'/typography/thirdparty/colourchart/csscolorchart.php'); - $cssPath = array($baseFolder.'/themes/', $baseFolder.$this->project()."css/"); - echo '

CSS colors found in: ' . - (is_array($cssPath)?implode($cssPath, ', '):$cssPath) . '

'; - $themes = new CssColorChart(); - $colourList = $themes->listColors($cssPath); - $html = DBField::create_field("HTMLText", $colourList); - echo $html; - } - - function Form() { - $array = array(); - $array[] = "green"; - $array[] = "yellow"; - $array[] = "blue"; - $array[] = "pink"; - $array[] = "orange"; - $errorField0 = new TextField($name = "ErrorField0", $title = "Text Field Example 1"); - $errorField0->setError("message shown when something is good", "good"); - $errorField1 = new TextField($name = "ErrorField1", $title = "Text Field Example 2"); - $errorField1->setError("message shown when something is bad", "bad"); - $errorField2 = new TextField($name = "ErrorField2", $title = "Text Field Example 3"); - $errorField2->setCustomValidationMessage("message shown when something is bad", "bad"); - $errorField3 = new TextField($name = "ErrorField3", $title = "Text Field Example 4"); - $errorField3->setError("there is an error", "required"); - $errorField4 = new TextField($name = "ErrorField4", $title = "Text Field Example 5"); - $errorField4->setCustomValidationMessage("custom validation error"); - $rightTitle = new TextField($name = "RightTitleField", $title = "Left Title is Default"); - $rightTitle->setRightTitle("right title here"); - $readonlyField = new ReadOnlyField($name = "ReadOnlyField", $title = "ReadOnlyField"); - $readonlyField->setValue("read only value"); - $groupedDropdownField = new GroupedDropdownField( - $name = "dropdown", - $title = "Simple Grouped Dropdown", - $source = array( - "primary" => array( - "1" => "Green", - "2" => "Red", - "3" => "Blue", - "4" => "Orange" - ), - "secondary" => array( - "11" => "Light Blue", - "12" => "Dark Brown", - "13" => "Pinkish Red" - ) - ) - ); - $form = new Form( - $controller = $this, - $name = "TestForm", - $fields = new FieldList( - // List the your fields here - new HeaderField($name = "HeaderField1", $title = "Default Header Field"), - new LiteralField($name = "LiteralField", "

NOTE: All fields up to EmailField are required and should be marked as such

"), - new TextField($name = "TextField1", $title = "Text Field Example 1"), - new TextField($name = "TextField2", $title = "Text Field Example 2"), - new TextField($name = "TextField3", $title = "Text Field Example 3"), - new TextField($name = "TextField4", $title = ""), - new HeaderField($name = "HeaderField2a", $title = "Error Messages", 2), - new LiteralField($name = "ErrorExplanations", '

Below are some error messages, some of them only show up after you have placed your cursor on the field and not completed it (i.e. a reminder to complete the field)

'), - $errorField0, - $errorField1, - $errorField2, - $errorField3, - $errorField4, - new HeaderField($name = "HeaderField2b", $title = "Field with right title", 2), - $rightTitle, - $textAreaField = new TextareaField($name = "TextareaField", $title = "Textarea Field"), - new EmailField("EmailField", "Email address"), - new HeaderField($name = "HeaderField2c", $title = "HeaderField Level 2", 2), - new DropdownField($name = "DropdownField",$title = "Dropdown Field",array( 0 => "-- please select --", 1 => "test AAAA", 2 => "test BBBB")), - new OptionsetField($name = "OptionsetField",$title = "Optionset Field",$array), - new CheckboxSetField($name = "CheckboxSetField",$title = "Checkbox Set Field",$array), - new CurrencyField($name = "CurrencyField",$title = "Bling bling"), - new HeaderField($name = "HeaderField3", $title = "Other Fields", 3), - new NumericField($name = "NumericField", $title = "Numeric Field "), - new DateField($name = "DateField", $title = "Date Field"), - new DateField($name = "DateTimeField", $title = "Date and Time Field"), - new FileField($name = "FileField", $title = "File Field"), - new ConfirmedPasswordField($name = "ConfirmPasswordField", $title = "Password"), - new CheckboxField($name = "CheckboxField", $title = "Checkbox Field"), - $groupedDropdownField, - new HeaderField($name = "HeaderField4", $title = "Read-only Field", 3), - $readonlyField - ), - $actions = new FieldList( - // List the action buttons here - new FormAction("signup", "Sign up") - ), - $requiredFields = new RequiredFields( - "TextField1","TextField2", "TextField3","ErrorField1","ErrorField2", "EmailField", "TextField3", "RightTitleField", "CheckboxField", "CheckboxSetField" - ) - ); - $textAreaField->setColumns(7); - $form->setMessage("warning message", "warning"); - return $form; - } - - function TestForm($data) { - $this->redirectBack(); - } - - protected function typographyhtml() { - return $this->renderWith('TypographySample'); - } - - public function RandomLinkExternal(){ - return "http://www.google.com/?q=".rand(0,100000); - } - - public function RandomLinkInternal(){ - return "/?q=".rand(0,100000); - } - - public function SiteColours() { - if($folder = TypographyTestPage::get_css_folder()) { - Requirements::themedCSS("CssColorChart", "typography"); - Requirements::javascript("typography/javascript/CssColorChart.js"); - $cssColorChart = new CssColorChart(); - return $cssColorChart->listColors(Director::baseFolder()."/".$folder); - } - } - - function replacecolours() { - if($folder = Config::inst()->get("TypographyTestPage", "css_folder")) { - require_once(Director::baseFolder()."/typography/thirdparty/csscolorchart.php"); - $cssColorChart = new CssColorChart(); - return $cssColorChart->replaceColours(Director::baseFolder()."/".Config::inst()->get("TypographyTestPage", "css_folder")); - } - return "no folder specified, use TypographyTestPage::set_css_folder()"; - } - -} +get("TypographyTestPage", "css_folder")) { + $folder = Config::inst()->get("TypographyTestPage", "css_folder"); + } else { + $folder = "themes/".SSViewer::current_theme()."/css/"; + } + $fullFolder = Director::baseFolder().'/'.$folder; + if (!file_exists($fullFolder)) { + user_error("could not find the default CSS folder $fullFolder"); + $folder = ''; + } + return $folder; + } + + private static $defaults = array( + 'URLSegment' => 'typo', + 'ShowInMenus' => false, + 'ShowInSearch' => false, + 'Title' => 'Typography Test', + 'Content' => 'auto-completed - do not alter', + 'ShowInMenus' => false, + 'ShowInSearch' => false, + 'Sort' => 99999 + ); + + + public function canCreate($member = null) + { + if (TypographyTestPage::get()->First()) { + return false; + } + return parent::canCreate($member); + } + + public function requireDefaultRecords() + { + if (self::$auto_include) { + $className = $this->class; + $page = $className::get()->First(); + if (! $page) { + $page = new TypographyTestPage(self::$defaults); + $parent = SiteTree::get_by_link(self::$parent_url_segment); + if ($parent) { + $page->ParentID = $parent->ID; + } + $page->writeToStage('Stage'); + $page->publish('Stage', 'Live'); + $page->URLSegment = self::$defaults['URLSegment']; + $page->writeToStage('Stage'); + $page->publish('Stage', 'Live'); + DB::alteration_message('TypographyTestPage', 'created'); + } + } + } +} + +class TypographyTestPage_Controller extends Page_Controller +{ + + private static $allowed_actions = array( + "colours" => "ADMIN", + "replacecolours" => "ADMIN" + ); + + public function init() + { + parent::init(); + Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); + Requirements::javascript('typography/javascript/typography.js'); + } + + public function index() + { + $this->Content = $this->typographyhtml(); + return array(); + } + + public function ShowFirstHeading() + { + return Config::inst()->get("TypographyTestPage", "include_first_heading_in_test_copy"); + } + + public function colours() + { + $baseFolder = Director::baseFolder(); + require($baseFolder.'/typography/thirdparty/colourchart/csscolorchart.php'); + $cssPath = array($baseFolder.'/themes/', $baseFolder.$this->project()."css/"); + echo '

CSS colors found in: ' . + (is_array($cssPath)?implode($cssPath, ', '):$cssPath) . '

'; + $themes = new CssColorChart(); + $colourList = $themes->listColors($cssPath); + $html = DBField::create_field("HTMLText", $colourList); + echo $html; + } + + public function Form() + { + $array = array(); + $array[] = "green"; + $array[] = "yellow"; + $array[] = "blue"; + $array[] = "pink"; + $array[] = "orange"; + $errorField0 = new TextField($name = "ErrorField0", $title = "Text Field Example 1"); + $errorField0->setError("message shown when something is good", "good"); + $errorField1 = new TextField($name = "ErrorField1", $title = "Text Field Example 2"); + $errorField1->setError("message shown when something is bad", "bad"); + $errorField2 = new TextField($name = "ErrorField2", $title = "Text Field Example 3"); + $errorField2->setCustomValidationMessage("message shown when something is bad", "bad"); + $errorField3 = new TextField($name = "ErrorField3", $title = "Text Field Example 4"); + $errorField3->setError("there is an error", "required"); + $errorField4 = new TextField($name = "ErrorField4", $title = "Text Field Example 5"); + $errorField4->setCustomValidationMessage("custom validation error"); + $rightTitle = new TextField($name = "RightTitleField", $title = "Left Title is Default"); + $rightTitle->setRightTitle("right title here"); + $readonlyField = new ReadOnlyField($name = "ReadOnlyField", $title = "ReadOnlyField"); + $readonlyField->setValue("read only value"); + $groupedDropdownField = new GroupedDropdownField( + $name = "dropdown", + $title = "Simple Grouped Dropdown", + $source = array( + "primary" => array( + "1" => "Green", + "2" => "Red", + "3" => "Blue", + "4" => "Orange" + ), + "secondary" => array( + "11" => "Light Blue", + "12" => "Dark Brown", + "13" => "Pinkish Red" + ) + ) + ); + $form = new Form( + $controller = $this, + $name = "TestForm", + $fields = new FieldList( + // List the your fields here + new HeaderField($name = "HeaderField1", $title = "Default Header Field"), + new LiteralField($name = "LiteralField", "

NOTE: All fields up to EmailField are required and should be marked as such

"), + new TextField($name = "TextField1", $title = "Text Field Example 1"), + new TextField($name = "TextField2", $title = "Text Field Example 2"), + new TextField($name = "TextField3", $title = "Text Field Example 3"), + new TextField($name = "TextField4", $title = ""), + new HeaderField($name = "HeaderField2a", $title = "Error Messages", 2), + new LiteralField($name = "ErrorExplanations", '

Below are some error messages, some of them only show up after you have placed your cursor on the field and not completed it (i.e. a reminder to complete the field)

'), + $errorField0, + $errorField1, + $errorField2, + $errorField3, + $errorField4, + new HeaderField($name = "HeaderField2b", $title = "Field with right title", 2), + $rightTitle, + $textAreaField = new TextareaField($name = "TextareaField", $title = "Textarea Field"), + new EmailField("EmailField", "Email address"), + new HeaderField($name = "HeaderField2c", $title = "HeaderField Level 2", 2), + new DropdownField($name = "DropdownField", $title = "Dropdown Field", array( 0 => "-- please select --", 1 => "test AAAA", 2 => "test BBBB")), + new OptionsetField($name = "OptionsetField", $title = "Optionset Field", $array), + new CheckboxSetField($name = "CheckboxSetField", $title = "Checkbox Set Field", $array), + new CurrencyField($name = "CurrencyField", $title = "Bling bling"), + new HeaderField($name = "HeaderField3", $title = "Other Fields", 3), + new NumericField($name = "NumericField", $title = "Numeric Field "), + new DateField($name = "DateField", $title = "Date Field"), + new DateField($name = "DateTimeField", $title = "Date and Time Field"), + new FileField($name = "FileField", $title = "File Field"), + new ConfirmedPasswordField($name = "ConfirmPasswordField", $title = "Password"), + new CheckboxField($name = "CheckboxField", $title = "Checkbox Field"), + $groupedDropdownField, + new HeaderField($name = "HeaderField4", $title = "Read-only Field", 3), + $readonlyField + ), + $actions = new FieldList( + // List the action buttons here + new FormAction("signup", "Sign up") + ), + $requiredFields = new RequiredFields( + "TextField1", "TextField2", "TextField3", "ErrorField1", "ErrorField2", "EmailField", "TextField3", "RightTitleField", "CheckboxField", "CheckboxSetField" + ) + ); + $textAreaField->setColumns(7); + $form->setMessage("warning message", "warning"); + return $form; + } + + public function TestForm($data) + { + $this->redirectBack(); + } + + protected function typographyhtml() + { + return $this->renderWith('TypographySample'); + } + + public function RandomLinkExternal() + { + return "http://www.google.com/?q=".rand(0, 100000); + } + + public function RandomLinkInternal() + { + return "/?q=".rand(0, 100000); + } + + public function SiteColours() + { + if ($folder = TypographyTestPage::get_css_folder()) { + Requirements::themedCSS("CssColorChart", "typography"); + Requirements::javascript("typography/javascript/CssColorChart.js"); + $cssColorChart = new CssColorChart(); + return $cssColorChart->listColors(Director::baseFolder()."/".$folder); + } + } + + public function replacecolours() + { + if ($folder = Config::inst()->get("TypographyTestPage", "css_folder")) { + require_once(Director::baseFolder()."/typography/thirdparty/csscolorchart.php"); + $cssColorChart = new CssColorChart(); + return $cssColorChart->replaceColours(Director::baseFolder()."/".Config::inst()->get("TypographyTestPage", "css_folder")); + } + return "no folder specified, use TypographyTestPage::set_css_folder()"; + } +} diff --git a/code/api/CssColorChart.php b/code/api/CssColorChart.php index 7da1471..e8be592 100644 --- a/code/api/CssColorChart.php +++ b/code/api/CssColorChart.php @@ -1,502 +1,529 @@ matchResults = array(); - - $cssFiles = array(); - if (!is_array($dir)) $dir = array($dir); - foreach ($dir as $d) - $cssFiles = array_merge($cssFiles, $this->findCssFiles($d, '*.css')); - - $this->colorNames = $this->genColorNames(); - - $reg = array(); - foreach ($this->colorNames as $cn => $hex) - $reg[] = preg_quote($cn, '/'); - $this->colorRegex = implode('|', $reg); - - $ignore = array(); - foreach ($this->ignoreFiles as $i) - $ignore[] = preg_quote($i, '/'); - - $ignoreRegex = '('.implode($ignore, '|').')'; - - foreach ($cssFiles as $cssFile) { - if (strlen($ignoreRegex) == 2 || !preg_match('/'.$ignoreRegex.'/', $cssFile)) - $colors = $this->findColors($cssFile); - } - - $this->sortMatches(); - - return $this->displayColors(); - } - - /* Returns an array of all found *.css files in path - * @param String - * @return generated HTML of colour codes - */ - protected function findCssFiles($dir, $pattern) { - if (is_file($dir)) return array($dir); - $dir = rtrim(escapeshellcmd($dir), '/'); - $files = glob($dir.'/'.$pattern); - $subDirs = glob("$dir/{.[^.]*,*}", GLOB_BRACE|GLOB_ONLYDIR); - if($subDirs && is_array($subDirs)) { - foreach ($subDirs as $subDir) { - $arr = $this->findCssFiles($subDir, $pattern); - $files = array_merge($files, $arr); - } - } - return $files; - } - - /* Parse CSS files and build $this->matchResults - * @param String - * @return Null - */ - protected function findColors($cssFile) { - $data = $this->cssPrepare(file_get_contents($cssFile)); - - /* read line-by-line */ - $lines = preg_split('/\n/', $data, false, PREG_SPLIT_NO_EMPTY); - - foreach ($lines as $line) { - @preg_match('/^(.*)\{(.*)\}$/', $line, $matches); - if ($matches && count($matches) == 3) { - $className = $matches[1]; - $styles = preg_split('/\;/', $matches[2], false, PREG_SPLIT_NO_EMPTY); - - if ($styles) { - foreach ($styles as $declaration) { - $parts = preg_split('/\:/', $declaration, 2); - if (count($parts) == 2) { - $property = trim($parts[0]); - $value = trim($parts[1]); - /* Hex colors */ - if (preg_match_all('/(#[a-f0-9]{3,6})/i', $value, $colorMatch)) { - for ($x = 0; $x < count($colorMatch[0]); $x++) { - $matchLength = strlen($colorMatch[1][$x]); - if ($matchLength == 7 || $matchLength == 4) { - if ($matchLength == 4) - $colorMatch[0][$x] = $colorMatch[0][$x].str_replace('#', '', $colorMatch[0][$x]); - $this->addToMatchResults( - array( - 'color' => strtolower($colorMatch[0][$x]), - 'stylesheet' => $cssFile, - 'class' => $className, - 'property' => $property, - 'value' => $value - ) - ); - } - } - } - /* named colors */ - else if (preg_match_all('/\b('.$this->colorRegex.')\b/i', $value, $colorMatch)) { - for ($x = 0; $x < count($colorMatch[0]); $x++) { - $this->addToMatchResults( - array( - 'color' => strtolower($this->colorNames[strtolower($colorMatch[1][$x])]), - 'stylesheet' => $cssFile, - 'class' => $className, - 'property' => $property, - 'value' => $value - ) - ); - } - } - /* rgb colors */ - else if (preg_match_all('/\brgba?\s?\(\s?(\d+)\s?,\s?(\d+)\s?,\s?(\d+)\s?(,\s?(\d+)\s?)?\)/i', $value, $colorMatch)) { - for ($x = 0; $x < count($colorMatch[0]); $x++){ - $hexcolor = $this->rgb2hex($colorMatch[1][$x], $colorMatch[2][$x], $colorMatch[3][$x]); - $this->addToMatchResults( - array( - 'color' => '#'.$hexcolor, - 'stylesheet' => $cssFile, - 'class' => $className, - 'property' => $property, - 'value' => $value - ) - ); - } - } - } - } - } - } - } - - } - - /** - * Sort $this->matchResults by HSV values - * @param Null - * @return Null - */ - protected function sortMatches() { - if (count($this->matchResults) == 0) return array(); - - foreach ($this->matchResults as $code => $matches) { - $rgb = $this->hex2rgb($code); - $order[] = $this->rgb2hsv($rgb[0], $rgb[1], $rgb[2]); - } - //Split each array up into H, S and V arrays - foreach ($order as $k => $v) { - $hue[$k] = $v[0]; - $sat[$k] = $v[1]; - $val[$k] = $v[2]; - } - - //Sort in ascending order by H, then S, then V and recompile the array - array_multisort($hue, SORT_ASC, $sat, SORT_ASC, $val, SORT_ASC, $order); - - $output = array(); - foreach ($order as $k => $v) { - list($hue,$sat,$val) = $v; - $rgb = $this->hsv2rgb($hue,$sat,$val); - $hexcolor = $this->rgb2hex($rgb[0], $rgb[1], $rgb[2]); - $output[$hexcolor] = $this->matchResults[$hexcolor]; - } - - $this->matchResults = $output; - } - - /** - * Add array to $this->matchResults - * @param Array $arr - * @return Null - */ - protected function addToMatchResults(Array $arr) { - $color = str_replace('#', '', $arr['color']); - if (!isset($this->matchResults[$color])) $this->matchResults[$color] = array(); - array_push($this->matchResults[$color], $arr); - } - - /** - * Return HTML code of $this->matchResults - * @return String HTML - */ - protected function displayColors(){ - $out = ''; - foreach ($this->matchResults as $color => $matches) { - $out .= '
+class CssColorChart +{ + + /* + * list of files that are not included (e.g. /var/www/webroot/themes/myfile.css) + * @var Array + */ + public $ignoreFiles = array(); + + /** + * Find, parse, and return output + * @param String | Array $dir + * @return String generated HTML of colour codes + */ + public function listColors($dir) + { + $this->matchResults = array(); + + $cssFiles = array(); + if (!is_array($dir)) { + $dir = array($dir); + } + foreach ($dir as $d) { + $cssFiles = array_merge($cssFiles, $this->findCssFiles($d, '*.css')); + } + + $this->colorNames = $this->genColorNames(); + + $reg = array(); + foreach ($this->colorNames as $cn => $hex) { + $reg[] = preg_quote($cn, '/'); + } + $this->colorRegex = implode('|', $reg); + + $ignore = array(); + foreach ($this->ignoreFiles as $i) { + $ignore[] = preg_quote($i, '/'); + } + + $ignoreRegex = '('.implode($ignore, '|').')'; + + foreach ($cssFiles as $cssFile) { + if (strlen($ignoreRegex) == 2 || !preg_match('/'.$ignoreRegex.'/', $cssFile)) { + $colors = $this->findColors($cssFile); + } + } + + $this->sortMatches(); + + return $this->displayColors(); + } + + /* Returns an array of all found *.css files in path + * @param String + * @return generated HTML of colour codes + */ + protected function findCssFiles($dir, $pattern) + { + if (is_file($dir)) { + return array($dir); + } + $dir = rtrim(escapeshellcmd($dir), '/'); + $files = glob($dir.'/'.$pattern); + $subDirs = glob("$dir/{.[^.]*,*}", GLOB_BRACE|GLOB_ONLYDIR); + if ($subDirs && is_array($subDirs)) { + foreach ($subDirs as $subDir) { + $arr = $this->findCssFiles($subDir, $pattern); + $files = array_merge($files, $arr); + } + } + return $files; + } + + /* Parse CSS files and build $this->matchResults + * @param String + * @return Null + */ + protected function findColors($cssFile) + { + $data = $this->cssPrepare(file_get_contents($cssFile)); + + /* read line-by-line */ + $lines = preg_split('/\n/', $data, false, PREG_SPLIT_NO_EMPTY); + + foreach ($lines as $line) { + @preg_match('/^(.*)\{(.*)\}$/', $line, $matches); + if ($matches && count($matches) == 3) { + $className = $matches[1]; + $styles = preg_split('/\;/', $matches[2], false, PREG_SPLIT_NO_EMPTY); + + if ($styles) { + foreach ($styles as $declaration) { + $parts = preg_split('/\:/', $declaration, 2); + if (count($parts) == 2) { + $property = trim($parts[0]); + $value = trim($parts[1]); + /* Hex colors */ + if (preg_match_all('/(#[a-f0-9]{3,6})/i', $value, $colorMatch)) { + for ($x = 0; $x < count($colorMatch[0]); $x++) { + $matchLength = strlen($colorMatch[1][$x]); + if ($matchLength == 7 || $matchLength == 4) { + if ($matchLength == 4) { + $colorMatch[0][$x] = $colorMatch[0][$x].str_replace('#', '', $colorMatch[0][$x]); + } + $this->addToMatchResults( + array( + 'color' => strtolower($colorMatch[0][$x]), + 'stylesheet' => $cssFile, + 'class' => $className, + 'property' => $property, + 'value' => $value + ) + ); + } + } + } + /* named colors */ + elseif (preg_match_all('/\b('.$this->colorRegex.')\b/i', $value, $colorMatch)) { + for ($x = 0; $x < count($colorMatch[0]); $x++) { + $this->addToMatchResults( + array( + 'color' => strtolower($this->colorNames[strtolower($colorMatch[1][$x])]), + 'stylesheet' => $cssFile, + 'class' => $className, + 'property' => $property, + 'value' => $value + ) + ); + } + } + /* rgb colors */ + elseif (preg_match_all('/\brgba?\s?\(\s?(\d+)\s?,\s?(\d+)\s?,\s?(\d+)\s?(,\s?(\d+)\s?)?\)/i', $value, $colorMatch)) { + for ($x = 0; $x < count($colorMatch[0]); $x++) { + $hexcolor = $this->rgb2hex($colorMatch[1][$x], $colorMatch[2][$x], $colorMatch[3][$x]); + $this->addToMatchResults( + array( + 'color' => '#'.$hexcolor, + 'stylesheet' => $cssFile, + 'class' => $className, + 'property' => $property, + 'value' => $value + ) + ); + } + } + } + } + } + } + } + } + + /** + * Sort $this->matchResults by HSV values + * @param Null + * @return Null + */ + protected function sortMatches() + { + if (count($this->matchResults) == 0) { + return array(); + } + + foreach ($this->matchResults as $code => $matches) { + $rgb = $this->hex2rgb($code); + $order[] = $this->rgb2hsv($rgb[0], $rgb[1], $rgb[2]); + } + //Split each array up into H, S and V arrays + foreach ($order as $k => $v) { + $hue[$k] = $v[0]; + $sat[$k] = $v[1]; + $val[$k] = $v[2]; + } + + //Sort in ascending order by H, then S, then V and recompile the array + array_multisort($hue, SORT_ASC, $sat, SORT_ASC, $val, SORT_ASC, $order); + + $output = array(); + foreach ($order as $k => $v) { + list($hue, $sat, $val) = $v; + $rgb = $this->hsv2rgb($hue, $sat, $val); + $hexcolor = $this->rgb2hex($rgb[0], $rgb[1], $rgb[2]); + $output[$hexcolor] = $this->matchResults[$hexcolor]; + } + + $this->matchResults = $output; + } + + /** + * Add array to $this->matchResults + * @param Array $arr + * @return Null + */ + protected function addToMatchResults(array $arr) + { + $color = str_replace('#', '', $arr['color']); + if (!isset($this->matchResults[$color])) { + $this->matchResults[$color] = array(); + } + array_push($this->matchResults[$color], $arr); + } + + /** + * Return HTML code of $this->matchResults + * @return String HTML + */ + protected function displayColors() + { + $out = ''; + foreach ($this->matchResults as $color => $matches) { + $out .= '
#'.$color.'
'; - foreach ($matches as $match){ - $out .= ''.$match['stylesheet']."\n"; - $out .= "\t".preg_replace('/,/', ",\n\t ", $match['class']) . " {\n\t\t" . - ''.htmlspecialchars($match['property']).': ' . - ''.htmlspecialchars($match['value']).";\n\t}\n"; - } - $out .= '
'; - } - return $out; - } - - /** - * Clean CSS code, strip comments & format one style per line - * @param String $buffer - * @return String - */ - public function cssPrepare($buffer) { - $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // Strip out comments - $buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer); - $buffer = preg_replace('/\s+/', ' ', $buffer); - $buffer = preg_replace('/,\s/', ',', $buffer); - $buffer = preg_replace('/\s?\:\s?/', ':', $buffer); - $buffer = preg_replace('/\s?\;\s?/', ';', $buffer); - $buffer = preg_replace('/\s?\{\s?/', '{', $buffer); - $buffer = preg_replace('/\s?\}\s?/', '}', $buffer); - $buffer = preg_replace('/\;?\}\s?/', ';}', $buffer); - $buffer = preg_replace('/\}/', "}\n", $buffer); // each entry on one line - return trim($buffer); - } - - /** - * Convert RGB array to hexidecimal string - * @param Array(red, green, blue) - * @return String - */ - public function rgb2hex($r, $g, $b) { - $out = ""; - foreach (array($r, $g, $b) as $c) { - $hex = base_convert($c, 10, 16); - $out .= ($c < 16) ? ("0".$hex) : $hex; - } - return $out; - } - - /** - * Convert hexidecimal string to RGB array - * @param String - * @return Array(red, green, blue) - */ - public function hex2rgb($hex) { - $hex = str_replace("#", "", $hex); - - if (strlen($hex) == 3) { - $r = hexdec(substr($hex,0,1).substr($hex,0,1)); - $g = hexdec(substr($hex,1,1).substr($hex,1,1)); - $b = hexdec(substr($hex,2,1).substr($hex,2,1)); - } else { - $r = hexdec(substr($hex,0,2)); - $g = hexdec(substr($hex,2,2)); - $b = hexdec(substr($hex,4,2)); - } - return array($r, $g, $b); - } - - /** - * Convert RGB array to HSV array - * @param Array(red, green, blue) - * @return Array(hue, saturation, value) - */ - public function rgb2hsv($r,$g,$b) { - //Convert RGB to HSV - $r /= 255; - $g /= 255; - $b /= 255; - $min = min($r,$g,$b); - $max = max($r,$g,$b); - - switch($max) { - case 0: - $h = $s = $v = 0; - break; - case $min: - $h = $s = 0; - $v = $max; - break; - default: - $delta = $max - $min; - if ($r == $max) - $h = 0 + ($g - $b) / $delta; - else if ($g == $max) - $h = 2 + ($b - $r) / $delta; - else - $h = 4 + ($r - $g) / $delta; - $h *= 60; - if ($h < 0 ) $h += 360; - $s = $delta / $max; - $v = $max; - } - return array($h,$s,$v); - } - - /** - * Convert HSV array to RGB array - * @param Array(hue, saturation, value) - * @return Array(red, green, blue) - */ - public function hsv2rgb($h,$s,$v) { - //Convert HSV to RGB - if ($s == 0) { - $r = $g = $b = $v; - } else { - $h /= 60.0; - $s = $s; - $v = $v; - - $hi = floor($h); - $f = $h - $hi; - $p = ($v * (1.0 - $s)); - $q = ($v * (1.0 - ($f * $s))); - $t = ($v * (1.0 - ((1.0 - $f) * $s))); - - switch($hi) { - case 0: $r = $v; $g = $t; $b = $p; break; - case 1: $r = $q; $g = $v; $b = $p; break; - case 2: $r = $p; $g = $v; $b = $t; break; - case 3: $r = $p; $g = $q; $b = $v; break; - case 4: $r = $t; $g = $p; $b = $v; break; - default: $r = $v; $g = $p; $b = $q; break; - } - } - return array( - (integer) ($r * 255 + 0.5), - (integer) ($g * 255 + 0.5), - (integer) ($b * 255 + 0.5) - ); - } - - /** - * Return an array of colour names and their hexidecimal values - * @return Array - */ - protected function genColorNames() { - return array( - "aliceblue" => "#f0f8ff", - "antiquewhite" => "#faebd7", - "aqua" => "#00ffff", - "aquamarine" => "#7fffd4", - "azure" => "#f0ffff", - "beige" => "#f5f5dc", - "bisque" => "#ffe4c4", - "black" => "#000000", - "blanchedalmond" => "#ffebcd", - "blue" => "#0000ff", - "blueviolet" => "#8a2be2", - "brown" => "#a52a2a", - "burlywood" => "#deb887", - "cadetblue" => "#5f9ea0", - "chartreuse" => "#7fff00", - "chocolate" => "#d2691e", - "coral" => "#ff7f50", - "cornflowerblue" => "#6495ed", - "cornsilk" => "#fff8dc", - "crimson" => "#dc143c", - "cyan" => "#00ffff", - "darkblue" => "#00008b", - "darkcyan" => "#008b8b", - "darkgoldenrod" => "#b8860b", - "darkgray" => "#a9a9a9", - "darkgrey" => "#a9a9a9", - "darkgreen" => "#006400", - "darkkhaki" => "#bdb76b", - "darkmagenta" => "#8b008b", - "darkolivegreen" => "#556b2f", - "darkorange" => "#ff8c00", - "darkorchid" => "#9932cc", - "darkred" => "#8b0000", - "darksalmon" => "#e9967a", - "darkseagreen" => "#8fbc8f", - "darkslateblue" => "#483d8b", - "darkslategray" => "#2f4f4f", - "darkslategrey" => "#2f4f4f", - "darkturquoise" => "#00ced1", - "darkviolet" => "#9400d3", - "deeppink" => "#ff1493", - "deepskyblue" => "#00bfff", - "dimgray" => "#696969", - "dimgrey" => "#696969", - "dodgerblue" => "#1e90ff", - "firebrick" => "#b22222", - "floralwhite" => "#fffaf0", - "forestgreen" => "#228b22", - "fuchsia" => "#ff00ff", - "gainsboro" => "#dcdcdc", - "ghostwhite" => "#f8f8ff", - "gold" => "#ffd700", - "goldenrod" => "#daa520", - "gray" => "#808080", - "grey" => "#808080", - "green" => "#008000", - "greenyellow" => "#adff2f", - "honeydew" => "#f0fff0", - "hotpink" => "#ff69b4", - "indianred " => "#cd5c5c", - "indigo " => "#4b0082", - "ivory" => "#fffff0", - "khaki" => "#f0e68c", - "lavender" => "#e6e6fa", - "lavenderblush" => "#fff0f5", - "lawngreen" => "#7cfc00", - "lemonchiffon" => "#fffacd", - "lightblue" => "#add8e6", - "lightcoral" => "#f08080", - "lightcyan" => "#e0ffff", - "lightgoldenrodyellow" => "#fafad2", - "lightgray" => "#d3d3d3", - "lightgrey" => "#d3d3d3", - "lightgreen" => "#90ee90", - "lightpink" => "#ffb6c1", - "lightsalmon" => "#ffa07a", - "lightseagreen" => "#20b2aa", - "lightskyblue" => "#87cefa", - "lightslategray" => "#778899", - "lightslategrey" => "#778899", - "lightsteelblue" => "#b0c4de", - "lightyellow" => "#ffffe0", - "lime" => "#00ff00", - "limegreen" => "#32cd32", - "linen" => "#faf0e6", - "magenta" => "#ff00ff", - "maroon" => "#800000", - "mediumaquamarine" => "#66cdaa", - "mediumblue" => "#0000cd", - "mediumorchid" => "#ba55d3", - "mediumpurple" => "#9370db", - "mediumseagreen" => "#3cb371", - "mediumslateblue" => "#7b68ee", - "mediumspringgreen" => "#00fa9a", - "mediumturquoise" => "#48d1cc", - "mediumvioletred" => "#c71585", - "midnightblue" => "#191970", - "mintcream" => "#f5fffa", - "mistyrose" => "#ffe4e1", - "moccasin" => "#ffe4b5", - "navajowhite" => "#ffdead", - "navy" => "#000080", - "oldlace" => "#fdf5e6", - "olive" => "#808000", - "olivedrab" => "#6b8e23", - "orange" => "#ffa500", - "orangered" => "#ff4500", - "orchid" => "#da70d6", - "palegoldenrod" => "#eee8aa", - "palegreen" => "#98fb98", - "paleturquoise" => "#afeeee", - "palevioletred" => "#db7093", - "papayawhip" => "#ffefd5", - "peachpuff" => "#ffdab9", - "peru" => "#cd853f", - "pink" => "#ffc0cb", - "plum" => "#dda0dd", - "powderblue" => "#b0e0e6", - "purple" => "#800080", - "red" => "#ff0000", - "rosybrown" => "#bc8f8f", - "royalblue" => "#4169e1", - "saddlebrown" => "#8b4513", - "salmon" => "#fa8072", - "sandybrown" => "#f4a460", - "seagreen" => "#2e8b57", - "seashell" => "#fff5ee", - "sienna" => "#a0522d", - "silver" => "#c0c0c0", - "skyblue" => "#87ceeb", - "slateblue" => "#6a5acd", - "slategray" => "#708090", - "slategrey" => "#708090", - "snow" => "#fffafa", - "springgreen" => "#00ff7f", - "steelblue" => "#4682b4", - "tan" => "#d2b48c", - "teal" => "#008080", - "thistle" => "#d8bfd8", - "tomato" => "#ff6347", - "turquoise" => "#40e0d0", - "violet" => "#ee82ee", - "wheat" => "#f5deb3", - "white" => "#ffffff", - "whitesmoke" => "#f5f5f5", - "yellow" => "#ffff00", - "yellowgreen" => "#9acd32" - ); - } - + foreach ($matches as $match) { + $out .= ''.$match['stylesheet']."\n"; + $out .= "\t".preg_replace('/,/', ",\n\t ", $match['class']) . " {\n\t\t" . + ''.htmlspecialchars($match['property']).': ' . + ''.htmlspecialchars($match['value']).";\n\t}\n"; + } + $out .= '
'; + } + return $out; + } + + /** + * Clean CSS code, strip comments & format one style per line + * @param String $buffer + * @return String + */ + public function cssPrepare($buffer) + { + $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // Strip out comments + $buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer); + $buffer = preg_replace('/\s+/', ' ', $buffer); + $buffer = preg_replace('/,\s/', ',', $buffer); + $buffer = preg_replace('/\s?\:\s?/', ':', $buffer); + $buffer = preg_replace('/\s?\;\s?/', ';', $buffer); + $buffer = preg_replace('/\s?\{\s?/', '{', $buffer); + $buffer = preg_replace('/\s?\}\s?/', '}', $buffer); + $buffer = preg_replace('/\;?\}\s?/', ';}', $buffer); + $buffer = preg_replace('/\}/', "}\n", $buffer); // each entry on one line + return trim($buffer); + } + + /** + * Convert RGB array to hexidecimal string + * @param Array(red, green, blue) + * @return String + */ + public function rgb2hex($r, $g, $b) + { + $out = ""; + foreach (array($r, $g, $b) as $c) { + $hex = base_convert($c, 10, 16); + $out .= ($c < 16) ? ("0".$hex) : $hex; + } + return $out; + } + + /** + * Convert hexidecimal string to RGB array + * @param String + * @return Array(red, green, blue) + */ + public function hex2rgb($hex) + { + $hex = str_replace("#", "", $hex); + + if (strlen($hex) == 3) { + $r = hexdec(substr($hex, 0, 1).substr($hex, 0, 1)); + $g = hexdec(substr($hex, 1, 1).substr($hex, 1, 1)); + $b = hexdec(substr($hex, 2, 1).substr($hex, 2, 1)); + } else { + $r = hexdec(substr($hex, 0, 2)); + $g = hexdec(substr($hex, 2, 2)); + $b = hexdec(substr($hex, 4, 2)); + } + return array($r, $g, $b); + } + + /** + * Convert RGB array to HSV array + * @param Array(red, green, blue) + * @return Array(hue, saturation, value) + */ + public function rgb2hsv($r, $g, $b) + { + //Convert RGB to HSV + $r /= 255; + $g /= 255; + $b /= 255; + $min = min($r, $g, $b); + $max = max($r, $g, $b); + + switch ($max) { + case 0: + $h = $s = $v = 0; + break; + case $min: + $h = $s = 0; + $v = $max; + break; + default: + $delta = $max - $min; + if ($r == $max) { + $h = 0 + ($g - $b) / $delta; + } elseif ($g == $max) { + $h = 2 + ($b - $r) / $delta; + } else { + $h = 4 + ($r - $g) / $delta; + } + $h *= 60; + if ($h < 0) { + $h += 360; + } + $s = $delta / $max; + $v = $max; + } + return array($h,$s,$v); + } + + /** + * Convert HSV array to RGB array + * @param Array(hue, saturation, value) + * @return Array(red, green, blue) + */ + public function hsv2rgb($h, $s, $v) + { + //Convert HSV to RGB + if ($s == 0) { + $r = $g = $b = $v; + } else { + $h /= 60.0; + $s = $s; + $v = $v; + + $hi = floor($h); + $f = $h - $hi; + $p = ($v * (1.0 - $s)); + $q = ($v * (1.0 - ($f * $s))); + $t = ($v * (1.0 - ((1.0 - $f) * $s))); + + switch ($hi) { + case 0: $r = $v; $g = $t; $b = $p; break; + case 1: $r = $q; $g = $v; $b = $p; break; + case 2: $r = $p; $g = $v; $b = $t; break; + case 3: $r = $p; $g = $q; $b = $v; break; + case 4: $r = $t; $g = $p; $b = $v; break; + default: $r = $v; $g = $p; $b = $q; break; + } + } + return array( + (integer) ($r * 255 + 0.5), + (integer) ($g * 255 + 0.5), + (integer) ($b * 255 + 0.5) + ); + } + + /** + * Return an array of colour names and their hexidecimal values + * @return Array + */ + protected function genColorNames() + { + return array( + "aliceblue" => "#f0f8ff", + "antiquewhite" => "#faebd7", + "aqua" => "#00ffff", + "aquamarine" => "#7fffd4", + "azure" => "#f0ffff", + "beige" => "#f5f5dc", + "bisque" => "#ffe4c4", + "black" => "#000000", + "blanchedalmond" => "#ffebcd", + "blue" => "#0000ff", + "blueviolet" => "#8a2be2", + "brown" => "#a52a2a", + "burlywood" => "#deb887", + "cadetblue" => "#5f9ea0", + "chartreuse" => "#7fff00", + "chocolate" => "#d2691e", + "coral" => "#ff7f50", + "cornflowerblue" => "#6495ed", + "cornsilk" => "#fff8dc", + "crimson" => "#dc143c", + "cyan" => "#00ffff", + "darkblue" => "#00008b", + "darkcyan" => "#008b8b", + "darkgoldenrod" => "#b8860b", + "darkgray" => "#a9a9a9", + "darkgrey" => "#a9a9a9", + "darkgreen" => "#006400", + "darkkhaki" => "#bdb76b", + "darkmagenta" => "#8b008b", + "darkolivegreen" => "#556b2f", + "darkorange" => "#ff8c00", + "darkorchid" => "#9932cc", + "darkred" => "#8b0000", + "darksalmon" => "#e9967a", + "darkseagreen" => "#8fbc8f", + "darkslateblue" => "#483d8b", + "darkslategray" => "#2f4f4f", + "darkslategrey" => "#2f4f4f", + "darkturquoise" => "#00ced1", + "darkviolet" => "#9400d3", + "deeppink" => "#ff1493", + "deepskyblue" => "#00bfff", + "dimgray" => "#696969", + "dimgrey" => "#696969", + "dodgerblue" => "#1e90ff", + "firebrick" => "#b22222", + "floralwhite" => "#fffaf0", + "forestgreen" => "#228b22", + "fuchsia" => "#ff00ff", + "gainsboro" => "#dcdcdc", + "ghostwhite" => "#f8f8ff", + "gold" => "#ffd700", + "goldenrod" => "#daa520", + "gray" => "#808080", + "grey" => "#808080", + "green" => "#008000", + "greenyellow" => "#adff2f", + "honeydew" => "#f0fff0", + "hotpink" => "#ff69b4", + "indianred " => "#cd5c5c", + "indigo " => "#4b0082", + "ivory" => "#fffff0", + "khaki" => "#f0e68c", + "lavender" => "#e6e6fa", + "lavenderblush" => "#fff0f5", + "lawngreen" => "#7cfc00", + "lemonchiffon" => "#fffacd", + "lightblue" => "#add8e6", + "lightcoral" => "#f08080", + "lightcyan" => "#e0ffff", + "lightgoldenrodyellow" => "#fafad2", + "lightgray" => "#d3d3d3", + "lightgrey" => "#d3d3d3", + "lightgreen" => "#90ee90", + "lightpink" => "#ffb6c1", + "lightsalmon" => "#ffa07a", + "lightseagreen" => "#20b2aa", + "lightskyblue" => "#87cefa", + "lightslategray" => "#778899", + "lightslategrey" => "#778899", + "lightsteelblue" => "#b0c4de", + "lightyellow" => "#ffffe0", + "lime" => "#00ff00", + "limegreen" => "#32cd32", + "linen" => "#faf0e6", + "magenta" => "#ff00ff", + "maroon" => "#800000", + "mediumaquamarine" => "#66cdaa", + "mediumblue" => "#0000cd", + "mediumorchid" => "#ba55d3", + "mediumpurple" => "#9370db", + "mediumseagreen" => "#3cb371", + "mediumslateblue" => "#7b68ee", + "mediumspringgreen" => "#00fa9a", + "mediumturquoise" => "#48d1cc", + "mediumvioletred" => "#c71585", + "midnightblue" => "#191970", + "mintcream" => "#f5fffa", + "mistyrose" => "#ffe4e1", + "moccasin" => "#ffe4b5", + "navajowhite" => "#ffdead", + "navy" => "#000080", + "oldlace" => "#fdf5e6", + "olive" => "#808000", + "olivedrab" => "#6b8e23", + "orange" => "#ffa500", + "orangered" => "#ff4500", + "orchid" => "#da70d6", + "palegoldenrod" => "#eee8aa", + "palegreen" => "#98fb98", + "paleturquoise" => "#afeeee", + "palevioletred" => "#db7093", + "papayawhip" => "#ffefd5", + "peachpuff" => "#ffdab9", + "peru" => "#cd853f", + "pink" => "#ffc0cb", + "plum" => "#dda0dd", + "powderblue" => "#b0e0e6", + "purple" => "#800080", + "red" => "#ff0000", + "rosybrown" => "#bc8f8f", + "royalblue" => "#4169e1", + "saddlebrown" => "#8b4513", + "salmon" => "#fa8072", + "sandybrown" => "#f4a460", + "seagreen" => "#2e8b57", + "seashell" => "#fff5ee", + "sienna" => "#a0522d", + "silver" => "#c0c0c0", + "skyblue" => "#87ceeb", + "slateblue" => "#6a5acd", + "slategray" => "#708090", + "slategrey" => "#708090", + "snow" => "#fffafa", + "springgreen" => "#00ff7f", + "steelblue" => "#4682b4", + "tan" => "#d2b48c", + "teal" => "#008080", + "thistle" => "#d8bfd8", + "tomato" => "#ff6347", + "turquoise" => "#40e0d0", + "violet" => "#ee82ee", + "wheat" => "#f5deb3", + "white" => "#ffffff", + "whitesmoke" => "#f5f5f5", + "yellow" => "#ffff00", + "yellowgreen" => "#9acd32" + ); + } }