/**
  * Standard modular run function for snippet hooks. Generates XHTML to insert into a page using AJAX.
  *
  * @return tempcode  The snippet
  */
 function run()
 {
     $theme = get_param('theme');
     $equation = get_param('css_equation');
     require_code('themewizard');
     $css_path = get_custom_file_base() . '/themes/' . $theme . '/css_custom/global.css';
     if (!file_exists($css_path)) {
         $css_path = get_file_base() . '/themes/default/css/global.css';
     }
     $css_file_contents = file_get_contents($css_path, FILE_TEXT);
     $seed = find_theme_seed($theme);
     $dark = strpos($css_file_contents, '#000000; /* {$,wizard, 100% W/B} */') !== false;
     $colours = calculate_theme($seed, $theme, 'equations', 'colours', $dark);
     $parsed_equation = parse_css_colour_expression($equation);
     if (is_null($parsed_equation)) {
         return make_string_tempcode('');
     }
     $answer = execute_css_colour_expression($parsed_equation, $colours[0]);
     if (is_null($answer)) {
         return make_string_tempcode('');
     }
     return make_string_tempcode('#' . $answer);
 }
Exemple #2
0
/**
 * Augment an array of CSS colours with colours that are derived actually inside the CSS-sheets.
 *
 * @param  array		Map of colours.
 * @param  ID_TEXT	The theme it's being generated from
 * @return array		A pair: extended map of colours, colour expression landscape
 */
function calculate_dynamic_css_colours($colours, $source_theme)
{
    $theme = filter_naughty($source_theme);
    $css_dir = $theme == 'default' ? 'css' : 'css_custom';
    $dh = opendir(get_file_base() . '/themes/' . $theme . '/' . $css_dir . '/');
    require_lang('themes');
    // First we build up our landscape
    $landscape = array();
    while (($sheet = readdir($dh)) !== false) {
        if (substr($sheet, -4) == '.css') {
            $path = get_file_base() . '/themes/' . $theme . '/' . $css_dir . '/' . $sheet;
            $contents = unixify_line_format(file_get_contents($path, FILE_TEXT));
            $matches = array();
            $num_matches = preg_match_all('#/\\* *\\{\\$,([^,\\n\\r\\$\']*),([^}{\\n\\r\\$\']*)\\}#', $contents, $matches);
            for ($i = 0; $i < $num_matches; $i++) {
                $parsed = parse_css_colour_expression($matches[2][$i]);
                if (!is_null($parsed)) {
                    //					  Colour name	  Parsed expression									 Full match string	 Final colour
                    $landscape[] = array($matches[1][$i], $parsed, substr($matches[0][$i], 6, strlen($matches[0][$i]) - 7), NULL);
                }
            }
        }
    }
    // Then we resolve our expressions
    $resolved_landscaped = array();
    $safety_count = 0;
    while (count($landscape) != 0) {
        foreach ($landscape as $i => $peak) {
            $peak[3] = execute_css_colour_expression($peak[1], $colours);
            if (!is_null($peak[3])) {
                $resolved_landscaped[] = $peak;
                unset($landscape[$i]);
                // Then we add to the colours array
                if ($peak[0] != 'wizard') {
                    $colours[$peak[0]] = $peak[3];
                }
            }
        }
        $safety_count++;
        if ($safety_count == 100) {
            $_landscape = '';
            foreach ($landscape as $x) {
                if ($_landscape != '') {
                    $_landscape .= '; ';
                }
                $_landscape .= $x[2];
            }
            fatal_exit(do_lang_tempcode('UNRESOLVABLE_COLOURS', escape_html($_landscape)));
        }
    }
    return array($colours, $resolved_landscaped);
}