Location: PHPKode > projects > top serious project > bco/functions/functions.color.php
<?php

/***************************************************************************
 *   Copyright 2003 Ian Meyer, Ian Pitcher
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/* h 0 - 6, s 0 - 1, v 0 - 1
   rgb 0 - 1 */

function bco_rgb2hsv($color)
{
  $rd = $color['red'];
  $gd = $color['green'];
  $bd = $color['blue'];

  $max = max($rd, $gd, $bd);
  $min = min($rd, $gd, $bd);
  
  $del = $max - $min;
  $v = $max;

  if ($max != 0.0) $s = ($del) / $max;
             else $s = 0.0;

  $h = 0.0;
  if ($s != 0.0) {
    $rc = ($max - $rd) / $del;
    $gc = ($max - $gd) / $del;
    $bc = ($max - $bd) / $del;

    if      ($rd==$max) $h = $bc - $gc;
    else if ($gd==$max) $h = 2 + $rc - $bc;
    else if ($bd==$max) $h = 4 + $gc - $rc;

    if ($h<0.0) $h += 6;
  }

  $hsv['hue'] = $h;
  $hsv['saturation'] = $s;
  $hsv['value'] = $v;
	
  return $hsv;

}

function bco_hsv2rgb($color)
{
  $h = $color['hue'];
  $s = $color['saturation'];
  $v = $color['value'];

   while ($h >= 6.0) $h = $h - 6.0;
   while ($h <  0.0) $h = $h + 6.0;
   $j = floor($h);
   $f = $h - $j;
   $p = $v * (1-$s);
   $q = $v * (1 - ($s*$f));
   $t = $v * (1 - ($s*(1 - $f)));
   
   switch ($j) {
    case 0:  $rd = $v;  $gd = $t;  $bd = $p;  break;
    case 1:  $rd = $q;  $gd = $v;  $bd = $p;  break;
    case 2:  $rd = $p;  $gd = $v;  $bd = $t;  break;
    case 3:  $rd = $p;  $gd = $q;  $bd = $v;  break;
    case 4:  $rd = $t;  $gd = $p;  $bd = $v;  break;
    case 5:  $rd = $v;  $gd = $p;  $bd = $q;  break;
    default: $rd = $v;  $gd = $t;  $bd = $p;  break;
   }

   $rgb['red'] = $rd;
   $rgb['green'] = $gd;
   $rgb['blue'] = $bd;

   return $rgb;
}

function bco_hex2rgb($color) 
{
	if (!ereg("#", $color)) {
		$color = "#{$color}";
    }

	$color = array
	(
		'red' => hexdec("$color[1]$color[2]") / 255,
		'green' => hexdec("$color[3]$color[4]") / 255,
		'blue' => hexdec("$color[5]$color[6]") / 255
	);

	return $color;
}

function bco_rgb2hex($color) {

		$red = sprintf("%02s", dechex($color['red'] * 255));
		$green = sprintf("%02s", dechex($color['green'] * 255));
		$blue = sprintf("%02s", dechex($color['blue'] * 255));

	$color = array
	(
		'red' => $red,
		'green' => $green,
		'blue' => $blue
	);

	return $color;
}

function bco_adjust_value ($color, $value) 
{
	$hsv = bco_rgb2hsv(bco_hex2rgb($color));
	
	if (($hsv['value'] + $value) >= 1) {
		$value = ($value - 1);
	}

	$hsv['value'] = $value + $hsv['value']; 

	$rgb = bco_rgb2hex(bco_hsv2rgb($hsv));
    // temp change?
	//return "#" . $rgb['red'] . $rgb['green'] . $rgb['blue'];
    return $rgb['red'] . $rgb['green'] . $rgb['blue'];
}

function bco_adjust_saturation ($color, $saturation) 
{
	$hsv = bco_rgb2hsv(bco_hex2rgb($color));
	
	if (($hsv['saturation'] + $saturation) >= 1) {
		$saturation = ($saturation - 1);
	}

	$hsv['saturation'] = $saturation + $hsv['saturation']; 

	$rgb = bco_rgb2hex(bco_hsv2rgb($hsv));
	// temp change?
    //return "#" . $rgb['red'] . $rgb['green'] . $rgb['blue'];
    return $rgb['red'] . $rgb['green'] . $rgb['blue'];
}

function bco_adjust_hue ($color, $hue) 
{
	$hsv = bco_rgb2hsv(bco_hex2rgb($color));
	
	if (($hsv['hue'] + $hue) >= 6) {
		$hue = ($hue - 6);
	}

	$hsv['hue'] = $hue + $hsv['hue'];

	$rgb = bco_rgb2hex(bco_hsv2rgb($hsv));
    // temp change ?
	//return "#" . $rgb['red'] . $rgb['green'] . $rgb['blue'];
    return $rgb['red'] . $rgb['green'] . $rgb['blue'];
}



function bco_inverse_color($color) 
{
	$color = ~hexdec(ereg_replace("#", "0x", $color)) & 0xFFFFFF;

	return sprintf("#%06s", dechex($color));
}

function bco_contrast($color) 
{
	if (!ereg("#", $color)) {
		$color = "#{$color}";
    }

	$hsv = bco_rgb2hsv(bco_hex2rgb(bco_inverse_color($color)));

	$lum = bco_luminance($color);

	if ($lum >= 128) {
		$hsv['value'] = 0.0125;
		$hsv['saturation'] = 0.875;
	} else {
		$hsv['value'] = 0.9375;
		$hsv['saturation'] = 0.025;
	}

	$rgb = bco_rgb2hex(bco_hsv2rgb($hsv));
    // temp change ?
	//return "#" . $rgb['red'] . $rgb['green'] . $rgb['blue'];
    return $rgb['red'] . $rgb['green'] . $rgb['blue'];
}

function bco_luminance($color) 
{
	if (!ereg("#", $color))
		$color = "#{$color}";

	$hsv = bco_rgb2hsv(bco_hex2rgb(bco_inverse_color($color)));

		$color = array
	(
		'red' => hexdec("$color[1]$color[2]"),
		'green' => hexdec("$color[3]$color[4]"),
		'blue' => hexdec("$color[5]$color[6]")
	);

//  luminance = 0.25*red + 0.625*green + 0.125*blue
	$lum = round((0.25*$color['red']) +
		(0.625*$color['green']) +
		(0.125*$color['blue']));
	return $lum;
}

function bco_get_mid_value($color1, $color2) 
{
	if (!ereg("#", $color1)) {
		$color = "#{$color1}";
    }
	if (!ereg("#", $color2)) {
		$color = "#{$color2}";
    }

	$hsv1 = bco_rgb2hsv(bco_hex2rgb($color1));
	$hsv2 = bco_rgb2hsv(bco_hex2rgb($color2));
	
	$max = max($hsv1['value'], $hsv2['value']);
	$min = min($hsv1['value'], $hsv2['value']);

	return abs((($max - $min) / 2) + $min);
}

function bco_get_mid_hue($color1, $color2) 
{
	if (!ereg("#", $color1)) {
		$color = "#{$color1}";
    }
	if (!ereg("#", $color2)) {
		$color = "#{$color2}";
    }

	$hsv1 = bco_rgb2hsv(bco_hex2rgb($color1));
	$hsv2 = bco_rgb2hsv(bco_hex2rgb($color2));
	
	$max = max($hsv1['hue'], $hsv2['hue']);
	$min = min($hsv1['hue'], $hsv2['hue']);

	return abs((($max - $min) / 2) + $min);
}

function bco_generate_stylesheet($colors)
{
    list($bgcolor,$tr1color,$tr2color,$formcolor,$mypostcolor,$fontface,$fontsize) = split(",",$colors);

    $text_main = bco_contrast($bgcolor);
    $link_main = $text_main;
    $vlink_main = $text_main;
    $hlink_main = $text_main;

    $form_textColor = bco_contrast($formcolor);

    $myposttextcolor = bco_contrast($mypostcolor);

    $text_tr1 = bco_contrast($tr1color);
    $link_tr1 = $text_tr1;
    $vlink_tr1 = bco_adjust_value($link_tr1, bco_get_mid_value($tr1color, $link_tr1));

    $text_tr2 = bco_contrast($tr2color);
    $link_tr2 = $text_tr2;
    $vlink_tr2 = bco_adjust_value($link_tr2, bco_get_mid_value($bg_tr2, $link_tr2));

    $my_text = bco_contrast($mypostcolor);
    $my_link = $my_text;
    $my_vlink = bco_adjust_hue(
                                bco_adjust_value(
                                $link_private,
                                bco_get_mid_value($mypostcolor, $my_link)),
                                bco_get_mid_hue($mypostcolor, $my_link) + 1
                                );
    $body_fontsize = $fontsize . "px";
    if ($fontsize <= 10) {
        $smallfontsize = $fontsize . "px";
    } else {
        $smallfontsize = $fontsize - 2 . "px";
    }

    $header_fontsize = $fontsize + 2 . "px";

    $header_fontsize = $fontsize + 2 . "px";

    $stylesheet  =  "\nbody { background-color: #$bgcolor; background-image: none; color: #$text_main; font-family: $fontface, arial, helvetica, sans-serif;";
    $stylesheet .=  "\nfont-size: $body_fontsize; }";
    $stylesheet .=  "\nform { margin: 0px; }";
    $stylesheet .=  "\n.header { text-decoration: none; background-color: #$bgcolor; background-image: none; color: #$text_main; font-weight: normal;}";
    $stylesheet .=  "\na:link { text-decoration: none; background-color: #$bgcolor; background-image; none; color: #$link_main; }";
    $stylesheet .=  "\na:visited { text-decoration: none; background-color: #$bgcolor; background-image: none; color: #$vlink_main; }";
    $stylesheet .=  "\na:hover { text-decoration: underline; background-color: #$bgcolor; background-image; none; color: #$hlink_main; }";
    $stylesheet .=  "\n.menu { text-decoration: none; background-color: #$bgcolor; background-image: none; color: #$text_main; font-weight: normal;}";
    $stylesheet .=  "\na.menu:link { text-decoration: none; background-color: #$bgcolor; background-image; none; color: #$link_main; }";
    $stylesheet .=  "\na.menu:visited { text-decoration: none; background-color: #$bgcolor; background-image: none; color: #$vlink_main; }";
    $stylesheet .=  "\na.menu:hover { text-decoration: none; background-color: #$bgcolor; background-image; none; color: #$hlink_main; }"; 
    $stylesheet .=  "\n.mypostthread { border-top: 1px solid #000000; border-bottom: 1px solid #000000; background-color: #$mypostcolor; background-image: none; color: #$myposttextcolor; }";
    $stylesheet .=  "\na.tr1link { text-decoration: underline; background-color: #$tr1color; background-image: none; color: #$link_tr1; }";
    $stylesheet .=  "\na.tr1link:visited { text-decoration: underline; background-color: #$tr1color; background-image: none; color: #$vlink_tr1; }";
    $stylesheet .=  "\na.tr1link:hover { text-decoration: none; background-color: #$tr1color; background-image: none; color: #$link_tr1; }"; 
    $stylesheet .=  "\n.tr2thread { border-top: 1px solid #000000; border-bottom: 1px solid #000000; background-color: #$tr2color; background-image: none; color: #$text_tr2; }";
    $stylesheet .=  "\n.threadtable { border-left: 1px solid #000000; border-right: 1px solid #000000; border-bottom: 1px solid #000000; }";
    $stylesheet .=  "\n.mypost { background-color: #$mypostcolor; background-image: none; color: #$myposttextcolor; }";
    $stylesheet .=  "\na.mypost { background-color: #$mypostcolor; background-image: none; color: #$my_link; }";
    $stylesheet .=  "\na.mypost:visited { background-color: #$mypostcolor; background-image: none; color: #$my_vlink; }";
    $stylesheet .=  "\na.mypost:hover { background-color: #$mypostcolor; background-image: none; color: #$my_link; }"; $stylesheet .=  "\n.button,.textfield { font-family: $fontface, arial, helvetica, sans-serif; color: #$form_textColor; border: 1px solid #000000; background-color: #$formcolor; background-image: none; font-size: $body_fontsize;}";
    $stylesheet .=  "\n.tr1 { background-color: #$tr1color; background-image: none; color: #$text_tr1; }";
    $stylesheet .=  "\na.tr1 { background-color: #$tr1color; background-image: none; color: #$link_tr1; }";
    $stylesheet .=  "\na.tr1:visited { background-color: #$tr1color; background-image: none; color: #$vlink_tr1; }";
    $stylesheet .=  "\na.tr1:hover { background-color: #$tr1color; background-image: none; color: #$link_tr1; }";
    $stylesheet .=  "\n.tr2 { background-color: #$tr2color; background-image: none; color: #$text_tr2; }";
    $stylesheet .=  "\na.tr2 { background-color: #$tr2color; background-image: none; color: #$link_tr2; }";
    $stylesheet .=  "\na.tr2:visited { background-color: #$tr2color; background-image: none; color: #$vlink_tr2; }";
    $stylesheet .=  "\na.tr2:hover { background-color: #$tr2color; background-image: none; color: #$link_tr2; }";
    $stylesheet .=  "\n.replytable {border: none; }";
    $stylesheet .=  "\n.smallfont { font-size: $smallfontsize; }";
    $stylesheet .=  "\n.newmessage { text-decoration: blink; }";
    $stylesheet .=  "\n.mono { font-family: monospace; font-size: $body_fontsize; }";
    $stylesheet .=  "\n.dashed { border: 1px #$mypostcolor dashed; }";
    $stylesheet .=  "\n.title { font-size: $header_fontsize; font-weight: bold; }";

    return $stylesheet;
// End function
}

?>
Return current item: top serious project