Scripts
Kleur Klasse
Vond ik wel makkelijk, spreekt verder voor zich denk ik
kleur-klasse
[code]<?php
class Kleur
{
/*
* Plek voor rood, groen en blauw
*/
public $iR;
public $iG;
public $iB;
/*
* object __construct ( string $sHex )
* $sHex kan met of zonder hekje ervoor
*
* object Kleur ( int $iR [ , int $iG [ , int $iB ] ] )
* object Kleur ( array ( int $iR [ , int $iG [ , int $iB ] ] ) )
* als $iG of $iB niet is opgegeven, dan wordt er vanuit gegaan dat $iR ==
* $iG == $iB
*/
public function __construct ( )
{
$aArgs = func_get_args ( );
if ( count ( $aArgs ) == 1 AND strlen ( $aArgs [ 0 ] ) >= 6 )
{
list ( $this->iR , $this->iG , $this->iB ) = str_split ( strlen ( $aArgs [ 0 ] ) == 6 ? $aArgs [ 0 ] : substr ( $aArgs [ 0 ] , 1 ) , 2 );
$this->iR = hexdec ( $this->iR );
$this->iG = hexdec ( $this->iG );
$this->iB = hexdec ( $this->iB );
}
else
{
switch ( count ( $aArgs ) )
{
case 3:
list ( $this->iR , $this->iG , $this->iB ) = $aArgs;
break;
default:
if ( is_array ( $aArgs [ 0 ] ) )
list ( $this->iR , $this->iG , $this->iB ) = $aArgs [ 0 ];
else
$this->iR = $this->iG = $this->iB = intval ( $aArgs [ 0 ] );
break;
}
}
$this->iR = $this->iR < 0 ? 0 : ( $this->iR > 255 ? 255 : $this->iR );
$this->iG = $this->iG < 0 ? 0 : ( $this->iG > 255 ? 255 : $this->iG );
$this->iB = $this->iB < 0 ? 0 : ( $this->iB > 255 ? 255 : $this->iB );
}
/*
* string getHex ( [ int $iR [ , int $iG [ , int $B ] ] ] )
*/
public function getHex ( $iR = null , $iG = null , $iB = null )
{
if ( is_null ( $iR ) )
return strtoupper (
str_pad ( dechex ( $this->iR ) , 2 , 0 , STR_PAD_LEFT ) .
str_pad ( dechex ( $this->iG ) , 2 , 0 , STR_PAD_LEFT ) .
str_pad ( dechex ( $this->iB ) , 2 , 0 , STR_PAD_LEFT )
);
else
{
$o = new Kleur ( $iR , $iG , $iB );
return $o->getHex ( );
}
}
/*
* string getHekjeHex ( [ int $iR [ , int $iG [ , int $B ] ] ] )
*/
public function getHekjeHex ( $iR = null , $iG = null , $iB = null )
{
if ( is_null ( $iR ) )
return '#' . $this->getHex ( );
else
{
$o = new Kleur ( $iR , $iG , $iB );
return $o->getHekjeHex ( );
}
}
/*
* string getKorteHex ( [ int $iR [ , int $iG [ , int $B ] ] ] )
*/
public function getKorteHex ( $iR = null , $iG = null , $iB = null )
{
if ( is_null ( $iR ) )
{
$sR = str_pad ( dechex ( $this->iR ) , 2 , 0 , STR_PAD_LEFT );
$sG = str_pad ( dechex ( $this->iG ) , 2 , 0 , STR_PAD_LEFT );
$sB = str_pad ( dechex ( $this->iB ) , 2 , 0 , STR_PAD_LEFT );
if (
substr ( $sR , 0 , 1 ) == substr ( $sR , 1 ) AND
substr ( $sG , 0 , 1 ) == substr ( $sG , 1 ) AND
substr ( $sB , 0 , 1 ) == substr ( $sB , 1 )
)
{
$sR = substr ( $sR , 1 );
$sG = substr ( $sG , 1 );
$sB = substr ( $sB , 1 );
}
$sHex = $sR . $sG . $sB;
return strlen ( $sHex ) == 3 ? $sHex : strtoupper ( $sHex );
}
else
{
$o = new Kleur ( $iR , $iG , $iB );
return $o->getKorteHex ( );
}
}
/*
* string getHekjeKorteHex ( [ int $iR [ , int $iG [ , int $B ] ] ] )
*/
public function getHekjeKorteHex ( $iR = null , $iG = null , $iB = null )
{
if ( is_null ( $iR ) )
return '#' . $this->getKorteHex ( );
else
{
$o = new Kleur ( $iR , $iG , $iB );
return $o->getHekjeKorteHex ( );
}
}
/*
* array getRgb ( [ $sHex ] )
*/
public function getRgb ( $sHex = null )
{
if ( is_null ( $sHex ) )
return array ( 'r' => $this->iR , 'g' => $this->iG , 'b' => $this->iB );
else
{
$o = new Kleur ( $sHex );
return $o->getRgb ( );
}
}
/*
* string getRgbK ( [ $sHex ] )
*/
public function getRgbK ( $sHex )
{
if ( is_null ( $sHex ) )
return $this->iR . ',' . $this->iG . ',' . $this->iR;
else
{
$o = new Kleur ( $sHex );
return $o->getRgbK ( );
}
}
/*
* string getCssRgb ( [ $sHex ] )
*/
public function getCssRgb ( $sHex )
{
if ( is_null ( $sHex ) )
return 'rgb(' . $this->getRgbK ( ) . ')';
else
{
$o = new Kleur ( $sHex );
return $o->getCssRgb ( );
}
}
/*
* Kleur randomKleur ( )
*/
public static function randomKleur ( )
{
$iR = rand ( 0 , 255 );
$iG = rand ( 0 , 255 );
$iB = rand ( 0 , 255 );
return new seld ( $iR , $iG , $iB );
}
/*
* Kleur randomGrijswaarde ( )
*/
public static function randomGrijswaarde ( )
{
$i = rand ( 0 , 255 );
return new self ( $i );
}
/*
* Kleur maakWebsafe ( [ int $iR [ , int $iG [ , int $B ] ] ] )
*/
public function maakWebsafe ( $iR = null , $iG = null , $iB = null )
{
$iRKopie = $iR;
/*
* Alle combinaties van 00, 33, 66, 99, CC, en FF
* Alle combinaties van 00, 51, 102, 153, 204 en 255 dus
*/
if ( is_null ( $iR ) )
{
$iR = $this->iR;
$iG = $this->iG;
$iB = $this->iB;
}
$iR = self::maakWebsafeEnkel ( $iR );
$iG = self::maakWebsafeEnkel ( $iG );
$iB = self::maakWebsafeEnkel ( $iB );
if ( is_null ( $iRKopie ) )
{
$this->iR = $iR;
$this->iG = $iG;
$this->iB = $iB;
}
return new self ( $iR , $iG , $iB );
}
/*
* int maakWebsafeEnkel ( int $i )
*
* geeft websafe waarde van $i, (r, g of b)
*/
private static function maakWebsafeEnkel ( $i )
{
switch ( $i )
{
case $i < 26:
return 0;
break;
case $i < 77:
return 51;
break;
case $i < 128:
return 102;
break;
case $i < 179:
return 153;
break;
case $i < 230:
return 204;
break;
default:
return 255;
break;
}
}
/*
* Kleur meng ( Kleur $oKleur1 [ , Kleur $oKleur2 [ , enz... ] ] )
*/
public static function meng ( )
{
$aArgs = func_get_args ( );
foreach ( $aArgs as $oKleur )
if ( $oKleur instanceof Kleur )
$aKleuren [ ] = $oKleur;
$iRTotaal = 0;
$iGTotaal = 0;
$iBTotaal = 0;
foreach ( $aKleuren as $oKleur )
{
$a = $oKleur->getRgb ( );
$iRTotaal += $a [ 'r' ];
$iGTotaal += $a [ 'g' ];
$iBTotaal += $a [ 'b' ];
}
$iTotaal = count ( $aKleuren );
$iR = round ( $iRTotaal / $iTotaal );
$iG = round ( $iGTotaal / $iTotaal );
$iB = round ( $iBTotaal / $iTotaal );
return new self ( $iR , $iG , $iB );
}
/*
* int naarGrijswaarde ( [ float $fR = 30 [ , float $fG = 59 [ , float $fB = 11 ] ] ] )
*
* float $fR, $fG en $fB zijn respectievelijk om het aantal procenten van
* rood, groen en blauw in te stellen
*/
public function naarGrijswaarde ( $fR = 30 , $fG = 59 , $fB = 11 )
{
$fGrijs = ( $this->iR * $fR + $this->iG * $fG + $this->iB * $fB ) / 100;
$this->iR = $this->iG = $this->iB = $iR = $iG = $iB = round ( $fGrijs );
return $iR;
}
/*
* Kleur inverteer ( [ int $iR [ , int $iG [ , int $B ] ] ] )
*/
public function inverteer ( $iR = null , $iG = null , $iB = null )
{
if ( is_null ( $iR ) )
{
$iR = $this->iR = 255 - $this->iR;
$iG = $this->iG = 255 - $this->iG;
$iB = $this->iB = 255 - $this->iB;
}
else
{
$iR = 255 - $iR;
$iG = 255 - $iG;
$iB = 255 - $iB;
}
return new self ( $iR , $iG , $iB );
}
/*
* Aliassen
*/
public function getKorteHekjeHex ( $iR = null , $iG = null , $iB = null ) { return is_null ( $iR ) ? $this->getHekjeKorteHex ( ) : self::getHekjeKorteHex ( $iR , $iG , $iB ); }
public function getKorteHexHekje ( $iR = null , $iG = null , $iB = null ) { return is_null ( $iR ) ? $this->getHekjeKorteHex ( ) : self::getHekjeKorteHex ( $iR , $iG , $iB ); }
public function getHexHekje ( $iR = null , $iG = null , $iB = null ) { return is_null ( $iR ) ? $this->getHekjeHex ( ) : self::getHekjeHex ( $iR , $iG , $iB ); }
public function getRgbKommas ( $sHex = null ) { return is_null ( $sHex ) ? $this->getRgbK ( ) : self::getRgbK ( $sHex ); }
public function getRgbCSS ( $sHex = null ) { return is_null ( $sHex ) ? $this->getCssRgb ( ) : self::getCssRgb ( $sHex ); }
}
?>[/code]
[b]Voorbeeld:[/b]
[code]<?php
error_reporting( E_ALL );
/*
* Even een functietje om kleuren weer te geven
*/
function echoKleur ( $m )
{
$sHex = $m;
if ( $m instanceof Kleur )
$sHex = $m->getHekjeHex ( );
echo '<div style="width: 50px; height: 50px; background: ' . $sHex . '"> </div>' . PHP_EOL;
}
/*
* We gaan geel met rood mengen (zou dus oranje op moeten leveren)
*/
$o = new Kleur ( 'FFFF00' );
$o2 = new Kleur ( 'FF0000' );
$oGemengd = Kleur::meng ( $o , $o2 );
$sHex = $oGemengd->getHekjeHex ( );
echoKleur ( $sHex ); // Een oranje div
$oGemengd->naarGrijswaarde ( );
echoKleur ( $oGemengd ); // Een grijze div
$o = new Kleur ( 1 , 26 , 239 );
echoKleur ( $o ); // Donkerblauw
$o->maakWebsafe ( );
echoKleur ( $o ); // Iets lichter blauw, namelijk websafe :-) (rgb(0,51,255))
$o = Kleur::maakWebsafe ( 1 , 26 , 239 ); // Statisch aanroepen
echoKleur ( $o ); // Zelfde resultaat
$o->inverteer ( );
echoKleur ( $o ); // Omgekeerd: geel
?>[/code]
Reacties
0