Noob question: assign color (hex) values via CSS ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Dazulrich
Forum Newbie
Posts: 8
Joined: Mon Oct 09, 2006 2:30 pm

Noob question: assign color (hex) values via CSS ??

Post by Dazulrich »

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

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??
Dazulrich
Forum Newbie
Posts: 8
Joined: Mon Oct 09, 2006 2:30 pm

Post by Dazulrich »

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....
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'm a bit sketchy on what you are getting at too but here are a couple of pointers.

Instead of...

Code: Select all

$bgColor = '#dd00dd';
$widgetBorderColor = '#f00';
$textColor = '#baa';
$params = array('bg'=> $bgColor ...
...consolidate them with an array...

Code: Select all

$cssColor = array('bg' => '#dd00dd', 'widgetBorder' => '#f00', 'textColor' => '#baa');
$params = $cssColor;
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 :)

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;
Dazulrich
Forum Newbie
Posts: 8
Joined: Mon Oct 09, 2006 2:30 pm

Post by Dazulrich »

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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:

Code: Select all

<link type="text/css" rel="stylesheet" href="style.php?sheet=1" />
Where the 'sheet' var (or whatever you make it out to be) happens to be the color/settings setter for the sheet.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

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
Ahh got ya now. How about:

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.
Dazulrich
Forum Newbie
Posts: 8
Joined: Mon Oct 09, 2006 2:30 pm

Post by Dazulrich »

thanks guys. I'll try that once I'll get home.
Dazulrich
Forum Newbie
Posts: 8
Joined: Mon Oct 09, 2006 2:30 pm

Post by Dazulrich »

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...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

$css = file_get_contents('external.css');
But why do you want to do this, what is your desired end output?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Dazulrich wrote:Everah,
I don't think I'll completely follow your suggestion...
I am crushed. :cry: Seriously, as long as you find something that works for you, use it. We all pitch in here to try to help. If one of us manages to help someone else, it is a success across the board.
Dazulrich
Forum Newbie
Posts: 8
Joined: Mon Oct 09, 2006 2:30 pm

Post by Dazulrich »

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
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Try using JavaScript to do this there are lots of tutorials on the net teaching how to this. It' s quite simple too.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

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.

Code: Select all

require_once 'SomeColors.php';
header('Content-Type: text/css');
echo <<< END
body {
   color:{$color['foo']}
}
p {
   border-color:{$color['bar']}
}
END;
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.
cinac
Forum Newbie
Posts: 9
Joined: Wed Nov 16, 2005 1:30 pm

Post by cinac »

Just my $0.02, but rather than using the color values directly, have the PHP add the relevant 'class="foo"' or 'id="foo"' to whatever you are outputting? That's what the CSS is for -- get away from using too many "magic numbers" in the code.
Post Reply