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, 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 > > > > > > > >
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php