[PHP] Reversing the Colour.

2002-10-09 Thread Alexis Antonakis

Hi,

I have a site whereby the user can select the colour of links for their
individual sections.

What I would like to do is to get the exact opposite colour of the one that
they choose and use that in the 'hover over' option of the a tag.

The value for the colour is stored in hex.

To add to this, the user can also choose the background colour. I have made
sure that they cannot have the same background colour as colour of the link,
but obviously if the background colour is the exact opposite of the link
colour, then when they hover over the link, it would 'disappear'. In this
case I would like to choose HALFWAY between the two sets of colours. I know
that this might not always produce the best colour combinations, but I'm
trying to get it so that the links always 'stand out'. I cannot let them
choose the colour of the 'Hover Over' option unfortunately.

Any suggestions as to how to achieve this, especially the calculations, or
for that matter improve upon it, would be most appreciated.

Regards
Alexis



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Reversing the Colour.

2002-10-09 Thread Pekka Saarinen

At 10/9/2002, you wrote:
Hi,

I have a site whereby the user can select the colour of links for their
individual sections.

What I would like to do is to get the exact opposite colour of the one that
they choose and use that in the 'hover over' option of the a tag.

The value for the colour is stored in hex.

To add to this, the user can also choose the background colour. I have made
sure that they cannot have the same background colour as colour of the link,
but obviously if the background colour is the exact opposite of the link
colour, then when they hover over the link, it would 'disappear'. In this
case I would like to choose HALFWAY between the two sets of colours. I know
that this might not always produce the best colour combinations, but I'm
trying to get it so that the links always 'stand out'. I cannot let them
choose the colour of the 'Hover Over' option unfortunately.

Any suggestions as to how to achieve this, especially the calculations, or
for that matter improve upon it, would be most appreciated.

Hi,

I struggled with the same issues a few months ago, and coded a function for 
brightening or darkening a HEX color.
Maybe it can help you in your dilemma.

The code converts HEX to RGB, multiplies each color channel
and converts back to HEX. This way the hue stays correct. The function
could of course have been written in more concise manner, but as I'm
only learning PHP more deeply, I like to write code that even I can
understand :)

function hexcolorshifter($color,$amount)
{
 $r = hexdec(substr($color, 0, 2));
 $g = hexdec(substr($color, 2, 2));
 $b = hexdec(substr($color, 4, 2));
 $new_r = abs(intval($r*$amount)); if ($new_r255) $new_r=255;
 $new_g = abs(intval($g*$amount)); if ($new_g255) $new_g=255;
 $new_b = abs(intval($b*$amount)); if ($new_b255) $new_b=255;
 $hex_r =  sprintf(%02X,$new_r);
 $hex_g =  sprintf(%02X,$new_g);
 $hex_b =  sprintf(%02X,$new_b);
 // print line is only for debugging:
 // print  R: . $new_r  . , .  $hex_r .  G: .
$new_g . , .  $hex_g .  B: . $new_b  . , .  $hex_b . br;
 $color = $hex_r .$hex_g .$hex_b;
 return $color;
}


Use it like this:
$my_bg_color = hexcolorshifter($my_bg_color,.8);

If you brighten a color, each channel will clip at 255, so in
situations where you have light source color you will most likely have
hue shifts if channels don't clip 'equally'.





-
Pekka Saarinen
http://photography-on-the.net
-



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Reversing the Colour.

2002-10-09 Thread ::[ Julien Bonastre ]::

Assumptions:
- background colour is stored in $bgcol (ie 4592FF)
- chosen link colour is stored in $lncol (ie *same as above*)
- Finds half-way colour between $bgcol and $lncol (returns a RRGGBB in hex
as above)

Notes:
This is actually fairly simple Alexis.. Simply convert your hex vals to
something you can work with (ie dec) using hexdec() and then do some basic
maths to find the half way between each of the R G B vals and then finally
spitting out a hex value for the midway mark..


Implementation:

?php
/** Func: getMidColour **/
// Returns the midway value in HEX between the two DEC values $dec1, $dec2
function getMidColour($dec1,$dec2) {
  return
dechex(abs(hexdec($dec1)-hexdec($dec2))/2+(hexdec($dec1)hexdec($dec2)?hexde
c($dec2):hexdec($dec1)));
}

// Parses 6char hex colour (RRGGBB) into 3 part RGB string and feeds into an
array
$bgcol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
bgcol));
$lncol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
lncol));

// We now can access the hex RGB values through the [0],[1],[2] elements of
these arrays..
// Using our custom function we can throw into our final array the actual 3
hex values
// desired which is the midpoint between $bgcol and $lncol
$midcol_arr=Array(getMidColour($bgcol_arr[0],$lncol_arr[0]),getMidColour($bg
col_arr[1],$lncol_arr[1]),getMidColour($bgcol_arr[2],$lncol_arr[2]));




---

HIH.. Not tested.. Please tell me how it goes (if it goes :-p)



Bye


- Original Message -
From: Alexis Antonakis [EMAIL PROTECTED]
To: Php-General@Lists. Php. Net [EMAIL PROTECTED]
Sent: Wednesday, October 09, 2002 10:48 PM
Subject: [PHP] Reversing the Colour.


 Hi,

 I have a site whereby the user can select the colour of links for their
 individual sections.

 What I would like to do is to get the exact opposite colour of the one
that
 they choose and use that in the 'hover over' option of the a tag.

 The value for the colour is stored in hex.

 To add to this, the user can also choose the background colour. I have
made
 sure that they cannot have the same background colour as colour of the
link,
 but obviously if the background colour is the exact opposite of the link
 colour, then when they hover over the link, it would 'disappear'. In this
 case I would like to choose HALFWAY between the two sets of colours. I
know
 that this might not always produce the best colour combinations, but I'm
 trying to get it so that the links always 'stand out'. I cannot let them
 choose the colour of the 'Hover Over' option unfortunately.

 Any suggestions as to how to achieve this, especially the calculations, or
 for that matter improve upon it, would be most appreciated.

 Regards
 Alexis



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php






Re: [PHP] Reversing the Colour.

2002-10-09 Thread ::[ Julien Bonastre ]::

Ok below is the fully tested and working code:

NOTE: . I have attached it too so wordwrapping email clients don't stuff the
lines up .. ;-)

This script will take in two 6character HTML hex colours (ie 44F0DD) and
return the midway colour between them (in Hex also)..


--
?php
//Example values..
$bgcol=39DFD9;
$lncol=F02816;

/** Func: getMidColour **/
// Returns the midway value in HEX between the two DEC values $dec1, $dec2
function getMidColour($dec1,$dec2) {
  return
dechex(abs(hexdec($dec1)-hexdec($dec2))/2+(hexdec($dec1)hexdec($dec2)?hexde
c($dec2):hexdec($dec1)));
}

// Parses 6char hex colour (RRGGBB) into 3 part RGB string and feeds into an
array
$bgcol_arr=split(,,preg_replace(/(.{2})(.{2})(.{2})/,$1,$2,$3,$bgcol))
;
$lncol_arr=split(,,preg_replace(/(.{2})(.{2})(.{2})/,$1,$2,$3,$lncol))
;

// We now can access the hex RGB values through the [0],[1],[2] elements of
these arrays..
// Using our custom function we can throw into our final array the actual 3
hex values
// desired which is the midpoint between $bgcol and $lncol
$midcol_arr=Array(getMidColour($bgcol_arr[0],$lncol_arr[0]),getMidColour($bg
col_arr[1],$lncol_arr[1]),getMidColour($bgcol_arr[2],$lncol_arr[2]));

// Formats the array into a string and Upper cases it..
$midcol=strtoupper(join(,$midcol_arr));

?
-

You then have the $midcol var which holds a uppercase HTML hex colour string
which represents the midway colour between $bgcol and $lncol..



Finally.. :-p


Ok HIH

- Original Message -
From: ::[ Julien Bonastre ]:: [EMAIL PROTECTED]
To: ::[ Julien Bonastre ]:: [EMAIL PROTECTED]
Sent: Thursday, October 10, 2002 12:19 AM
Subject: Re: [PHP] Reversing the Colour.


 Hang on..

 Yep I just tested it.

 Make sure you incldue the forward slash REGEX seperators in those preg
 functions..

 Ie.. my lines in previous email have:

$bgcol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
 bgcol));

 Which should be:

$bgcol_arr=split(,,preg_replace(/([\d]{2})([\d]{2})([\d]{2})/,$1,$2,$3
 ,$bgcol));



 Anyway...
 - Original Message -
 From: ::[ Julien Bonastre ]:: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Sent: Wednesday, October 09, 2002 11:59 PM
 Subject: Re: [PHP] Reversing the Colour.


 Assumptions:
 - background colour is stored in $bgcol (ie 4592FF)
 - chosen link colour is stored in $lncol (ie *same as above*)
 - Finds half-way colour between $bgcol and $lncol (returns a RRGGBB in
hex
 as above)

 Notes:
 This is actually fairly simple Alexis.. Simply convert your hex vals to
 something you can work with (ie dec) using hexdec() and then do some basic
 maths to find the half way between each of the R G B vals and then finally
 spitting out a hex value for the midway mark..


 Implementation:

 ?php
 /** Func: getMidColour **/
 // Returns the midway value in HEX between the two DEC values $dec1, $dec2
 function getMidColour($dec1,$dec2) {
   return

dechex(abs(hexdec($dec1)-hexdec($dec2))/2+(hexdec($dec1)hexdec($dec2)?hexde
 c($dec2):hexdec($dec1)));
 }

 // Parses 6char hex colour (RRGGBB) into 3 part RGB string and feeds into
an
 array

$bgcol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
 bgcol));

$lncol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
 lncol));

 // We now can access the hex RGB values through the [0],[1],[2] elements
of
 these arrays..
 // Using our custom function we can throw into our final array the actual
3
 hex values
 // desired which is the midpoint between $bgcol and $lncol

$midcol_arr=Array(getMidColour($bgcol_arr[0],$lncol_arr[0]),getMidColour($bg
 col_arr[1],$lncol_arr[1]),getMidColour($bgcol_arr[2],$lncol_arr[2]));




 ---

 HIH.. Not tested.. Please tell me how it goes (if it goes :-p)



 Bye


 - Original Message -
 From: Alexis Antonakis [EMAIL PROTECTED]
 To: Php-General@Lists. Php. Net [EMAIL PROTECTED]
 Sent: Wednesday, October 09, 2002 10:48 PM
 Subject: [PHP] Reversing the Colour.


  Hi,
 
  I have a site whereby the user can select the colour of links for their
  individual sections.
 
  What I would like to do is to get the exact opposite colour of the one
 that
  they choose and use that in the 'hover over' option of the a tag.
 
  The value for the colour is stored in hex.
 
  To add to this, the user can also choose the background colour. I have
 made
  sure that they cannot have the same background colour as colour of the
 link,
  but obviously if the background colour is the exact opposite of the link
  colour, then when they hover over the link, it would 'disappear'. In
this
  case I would like to choose HALFWAY between the two sets of colours. I
 know
  that this might not always produce the best colour combinations, but I'm
  trying to get it so that the links always 'stand out'. I cannot let them
  choose the colour of the 'Hover Over' option unfortunately.
 
  Any suggestions as to how to achieve this, especially the calculations

Re: [PHP] Reversing the Colour

2002-10-09 Thread ::[ Julien Bonastre ]::

Hang on..

Yep I just tested it.

Make sure you incldue the forward slash REGEX seperators in those preg
functions..

Ie.. my lines in previous email have:
$bgcol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
bgcol));

Which should be:
$bgcol_arr=split(,,preg_replace(/([\d]{2})([\d]{2})([\d]{2})/,$1,$2,$3
,$bgcol));



Anyway...
- Original Message -
From: ::[ Julien Bonastre ]:: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Wednesday, October 09, 2002 11:59 PM
Subject: Re: [PHP] Reversing the Colour.


Assumptions:
- background colour is stored in $bgcol (ie 4592FF)
- chosen link colour is stored in $lncol (ie *same as above*)
- Finds half-way colour between $bgcol and $lncol (returns a RRGGBB in hex
as above)

Notes:
This is actually fairly simple Alexis.. Simply convert your hex vals to
something you can work with (ie dec) using hexdec() and then do some basic
maths to find the half way between each of the R G B vals and then finally
spitting out a hex value for the midway mark..


Implementation:

?php
/** Func: getMidColour **/
// Returns the midway value in HEX between the two DEC values $dec1, $dec2
function getMidColour($dec1,$dec2) {
  return
dechex(abs(hexdec($dec1)-hexdec($dec2))/2+(hexdec($dec1)hexdec($dec2)?hexde
c($dec2):hexdec($dec1)));
}

// Parses 6char hex colour (RRGGBB) into 3 part RGB string and feeds into an
array
$bgcol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
bgcol));
$lncol_arr=split(,,preg_replace(([\d]{2})([\d]{2})([\d]{2}),$1,$2,$3,$
lncol));

// We now can access the hex RGB values through the [0],[1],[2] elements of
these arrays..
// Using our custom function we can throw into our final array the actual 3
hex values
// desired which is the midpoint between $bgcol and $lncol
$midcol_arr=Array(getMidColour($bgcol_arr[0],$lncol_arr[0]),getMidColour($bg
col_arr[1],$lncol_arr[1]),getMidColour($bgcol_arr[2],$lncol_arr[2]));




---

HIH.. Not tested.. Please tell me how it goes (if it goes :-p)



Bye


- Original Message -
From: Alexis Antonakis [EMAIL PROTECTED]
To: Php-General@Lists. Php. Net [EMAIL PROTECTED]
Sent: Wednesday, October 09, 2002 10:48 PM
Subject: [PHP] Reversing the Colour.


 Hi,

 I have a site whereby the user can select the colour of links for their
 individual sections.

 What I would like to do is to get the exact opposite colour of the one
that
 they choose and use that in the 'hover over' option of the a tag.

 The value for the colour is stored in hex.

 To add to this, the user can also choose the background colour. I have
made
 sure that they cannot have the same background colour as colour of the
link,
 but obviously if the background colour is the exact opposite of the link
 colour, then when they hover over the link, it would 'disappear'. In this
 case I would like to choose HALFWAY between the two sets of colours. I
know
 that this might not always produce the best colour combinations, but I'm
 trying to get it so that the links always 'stand out'. I cannot let them
 choose the colour of the 'Hover Over' option unfortunately.

 Any suggestions as to how to achieve this, especially the calculations, or
 for that matter improve upon it, would be most appreciated.

 Regards
 Alexis



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php