Noob question: assign color (hex) values via CSS ??
Moderator: General Moderators
Noob question: assign color (hex) values via CSS ??
Hi,
Its my first post - please be gentle...
I have some php code I want to clean up and my last issue are some hard coded color values, that I would like to move into the stylesheet.
It's a theme file that in turn passes values into a template file. The color values I am interested in, are passed as hex values as they are used in the template file not for class definition, but as hex values for mouseover function calls.
As I would like to make theme changes easy - I would like to see those values pulled from the CSS instead of modifying multiple files.
Is there any way to assign CSS property values to a PHP variable - instead of hard coding $bgcolor= '#DD00DD' ??
Or can I declare that variable in the CSS (using some php code) and use it in some php file.
Hope I explained that ok-ish.
TIA,
Daz
Its my first post - please be gentle...
I have some php code I want to clean up and my last issue are some hard coded color values, that I would like to move into the stylesheet.
It's a theme file that in turn passes values into a template file. The color values I am interested in, are passed as hex values as they are used in the template file not for class definition, but as hex values for mouseover function calls.
As I would like to make theme changes easy - I would like to see those values pulled from the CSS instead of modifying multiple files.
Is there any way to assign CSS property values to a PHP variable - instead of hard coding $bgcolor= '#DD00DD' ??
Or can I declare that variable in the CSS (using some php code) and use it in some php file.
Hope I explained that ok-ish.
TIA,
Daz
I'm not sure I understand what you are trying to do... so let me verify...
You have a file with color declarations stored in variables, and in the template files, it uses those variables to display color? If this is the case, why don't you just lose the file w/the variables and just replace it with a css file??
You have a file with color declarations stored in variables, and in the template files, it uses those variables to display color? If this is the case, why don't you just lose the file w/the variables and just replace it with a css file??
thanks for quick reply... let me try to explain better...
php file:
$bgcolor = '#dd00dd'
...
some other part of php code evaluating template with params like...
$params = array(
'{HOVER_BG_COLOR}' => $bgcolor,
....
)
echo template_eval()
in template file something like this...
<<<EOT
.......lots of other HTML code...
<td height='50' class='{DAY_CLASS}' align='center' valign='top' onmouseover="...., cOn(this,'{HOVER_BG_COLOR}');
.....
{DAY_CLASS} is passing the class name defined in the CSS. Which has all the definition like bg color or hover color in it. just don't know how to pull those out and pass into HOVER_BG_COLOR.
Like I said its existing code and I don't feel capable of rewriting it all....
php file:
$bgcolor = '#dd00dd'
...
some other part of php code evaluating template with params like...
$params = array(
'{HOVER_BG_COLOR}' => $bgcolor,
....
)
echo template_eval()
in template file something like this...
<<<EOT
.......lots of other HTML code...
<td height='50' class='{DAY_CLASS}' align='center' valign='top' onmouseover="...., cOn(this,'{HOVER_BG_COLOR}');
.....
{DAY_CLASS} is passing the class name defined in the CSS. Which has all the definition like bg color or hover color in it. just don't know how to pull those out and pass into HOVER_BG_COLOR.
Like I said its existing code and I don't feel capable of rewriting it all....
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
I'm a bit sketchy on what you are getting at too but here are a couple of pointers.
Instead of......consolidate them with an array...
Best thing of course is not to have any mention of colour in your PHP at all unless you have functionality relating to it and stick it all in the CSS. If you do have color functionality, such as random dynamic colours, make the css dynamic
Instead of...
Code: Select all
$bgColor = '#dd00dd';
$widgetBorderColor = '#f00';
$textColor = '#baa';
$params = array('bg'=> $bgColor ...Code: Select all
$cssColor = array('bg' => '#dd00dd', 'widgetBorder' => '#f00', 'textColor' => '#baa');
$params = $cssColor;Code: Select all
<?php
// Filename: main.css.php
header('Content-Type: text/css'); // important
$color = array('background' => '#dd00dd', 'widgetBorder' => '#f00', 'text' => '#baa');
echo <<< ENDCSS
body {
background-color:{$color['background']};
color:{$color['text']};
}
input {
border:solid 1px {$color['widgetBorder']};
}
ENDCSS;Ole,
I know i'm not the best to explain my problem...
maybe simplified one could say. instead of $variable='#FFF', i want to be able to pull the color value #FFF from a CSS definition and assign it to a PHP variable for further use. (to remove any color definitons from the PHP code)
And its NOT for use in body or TD tags...
thanks for the pointer...
I has seen the dynamic CSS stuff with variables somewhere - but couldn't figure out how to use those inside my php code - I thought they were limited to use inside the CSS file.
And yes, my only goal is to remove the color definitions from the php code and move them into the CSS.
I know i'm not the best to explain my problem...
maybe simplified one could say. instead of $variable='#FFF', i want to be able to pull the color value #FFF from a CSS definition and assign it to a PHP variable for further use. (to remove any color definitons from the PHP code)
And its NOT for use in body or TD tags...
thanks for the pointer...
I has seen the dynamic CSS stuff with variables somewhere - but couldn't figure out how to use those inside my php code - I thought they were limited to use inside the CSS file.
And yes, my only goal is to remove the color definitions from the php code and move them into the CSS.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You can do what you are asking, but you are going about it backwards. What you do is create a PHP file as a stylesheet and implement colors (and other CSS elements) in that file based upon a passed parameter. It usually looks something like:
Where the 'sheet' var (or whatever you make it out to be) happens to be the color/settings setter for the sheet.
Code: Select all
<link type="text/css" rel="stylesheet" href="style.php?sheet=1" />- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Ahh got ya now. How about:i want to be able to pull the color value #FFF from a CSS definition and assign it to a PHP variable for further use
Code: Select all
$css = 'body {
border:solid 1px #000;
color:#ff0000;
notamatch:#ff000; // invalid color
}
bar {
color:#baa;
}';
$matches = array();
$hex = '\da-f';
preg_match_all("/(?:#[$hex]{6}[^$hex]|#[$hex]{3}[^$hex])/i", $css, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);Code: Select all
Array
(
[0] => Array
(
[0] => Array
(
[0] => #000;
[1] => 25
)
[1] => Array
(
[0] => #ff0000;
[1] => 38
)
[2] => Array
(
[0] => #baa;
[1] => 98
)
)
)
Last edited by Ollie Saunders on Mon Oct 09, 2006 5:56 pm, edited 1 time in total.
Ole,
that works for inline CSS, but I haven't manage to make that work with external CSS file.... when I put that as php code into php parsed css file, I don't get anything.
But its a good starting point I guess. As I want to hand over more than just a single value.
I don't want to use inline CSS - otherwise I may leave it as is and mark it accordingly...
Everah,
I don't think I'll completely follow your suggestion...
that works for inline CSS, but I haven't manage to make that work with external CSS file.... when I put that as php code into php parsed css file, I don't get anything.
But its a good starting point I guess. As I want to hand over more than just a single value.
I don't want to use inline CSS - otherwise I may leave it as is and mark it accordingly...
Everah,
I don't think I'll completely follow your suggestion...
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
$css = file_get_contents('external.css');- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I spent some time playing around with it - and I feel like giving up on that idea.
Maybe one last attempt illustrating my desire...
I have a CSS file which I'd like to use for all color definitions (its long already).
I have 4 color defs inside a php file. $col1='#FFFFFF', col2='#..',etc.
I need the variables to have the hex value.
Is it possible to define a variable (well 4 actually) that are not used inside the CSS, but inside the php code.
I guess there is no way to define a global variable inside the CSS....
like (in php file) $cal1={body.background-color} - super simplified pseudo code
Oles suggestion works for inline CSS, but not when the CSS is an external file.
The loading via file_get_contents didn't work for me...
Unless there is something blatantly obvious, I'll give up and stick a comment into the CSS file where to find hardcoded color defs int he PHP file. sigh.
thanks for the help.
Daz
Maybe one last attempt illustrating my desire...
I have a CSS file which I'd like to use for all color definitions (its long already).
I have 4 color defs inside a php file. $col1='#FFFFFF', col2='#..',etc.
I need the variables to have the hex value.
Is it possible to define a variable (well 4 actually) that are not used inside the CSS, but inside the php code.
I guess there is no way to define a global variable inside the CSS....
like (in php file) $cal1={body.background-color} - super simplified pseudo code
Oles suggestion works for inline CSS, but not when the CSS is an external file.
The loading via file_get_contents didn't work for me...
Unless there is something blatantly obvious, I'll give up and stick a comment into the CSS file where to find hardcoded color defs int he PHP file. sigh.
thanks for the help.
Daz
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
You really should be generating the css dynamically as I showed in my first post on this thread. Remember when doing that you can still require in colours and consolldate them in one file. Using this method you can use the colours in other PHP code as well. If this isn't satisfactory to you please explain to me why it is not, I still don't feel as though I fully understand what you are trying to achieve.
Code: Select all
require_once 'SomeColors.php';
header('Content-Type: text/css');
echo <<< END
body {
color:{$color['foo']}
}
p {
border-color:{$color['bar']}
}
END;