Website Design United States, Website Design California, Website Designing United States, Website Designing California

Color Value Check Function - PHP

This function is the result of a web script I recently wrote. The idea was to allow visitors to my site to create their own XHTML/CSS "button" for the page. In the process of doing this script I realized that the values for various color features would need to be checked. I wanted to check both hex values (longhand and shorthand - like #ffeeff and #369) as well as the rgb(x,y,z) values to make sure they were valid colors. This code found here is the result.

This function returns a boolean value (true or false) so that it can be easily used in various statements.


Comments in the code are there to show you what is happening in the steps, they may be removed to save space when using. But hopefully the comments will help you learn.

Also note that I did not check for color name values like, "blue", "red", "teal", etc... feel free to modify the code for that if you'd like.


===== function color_check() facts =====
Color Check Function for Web Color Values (PHP 4)

Uses:
boolean check_color(string string [, string type]);

type should be:
hex - to represent a check for hexidecimal values
rgb - to represent a check for decimal rgb(x,y,z) values

the default "type" is "hex" if type is not specified


Colors in hex can be:
#XXYYZZ, #XYZ, XXYYZZ, or XYZ where values of 0-9 and a-f are the only values allowed.

Colors in rgb SHOULD be:
rgb(X,Y,Z) where X, Y and Z are decimal values between 0 and 255.


Examples for check_color() function:
====================================

if( check_color("fff") ){ echo "Color okay!"; }else{ echo "Color not okay!"; }
Output "Color okay!"

if( check_color("#zff, "hex"") ){ echo "Color okay!"; }else{ echo "Color not okay!"; }
Output "Color not okay!" - z is not valid in the hexidecimal values


if( check_color("rgb(102,51,0)") ){ echo "Color okay!"; }else{ echo "Color not okay!"; }
Output "Color not okay!" - "rgb" type not declaired, function is expecting a hex value


if( check_color("rgb(204,255,0)", "rgb") ){ echo "Color okay!"; }else{ echo "Color not okay!"; }
Output "Color okay!"
function check_color(){
  $numargs = func_num_args();
  if($numargs < 1){ echo "Error in function check_color(): No arguments passed to function."; return false; }
  $colors = func_get_arg(0);
  if($numargs == 2){ $type = func_get_arg(1); }else{ $type = "hex"; }
  if($numargs > 2){ echo "Error in function check_color(): Too many arguments passed to function."; return false; }

  // color types -
  //          hex - hexidecimal value in form of #XXX, XXX, #XXXXXX, or XXXXXX
  //          rgb - rgb value in form of rgb(X,Y,Z);
  if($type == "hex"){
    $colors = strtolower(str_replace("#", "", $colors));
    // \d is all decimal digits (0-9)
    $t = preg_match_all("([\da-f])", $colors, $matches);
    $length = strlen($colors);

    // Have to match the preg_match_all with the length to prevent incorrect results
    // else 3 matches in a 6 character code would make something like 123x98 valid
    if(($t == 3 and $length == 3) or ($t == 6 and $length == 6)){ return true; }else{ return false; }
  }elseif($type == "rgb"){
    $error = 0;
    // check the start and the end of the color code to make sure it's a valid form
    if((substr($colors, 0, 4)) != "rgb(" or substr($colors, -1) != ")"){ $error++; }
    else{
      $len = strlen($colors);
      // the full length - 5 characters to make up for the "rgb(" and the ")" - 5 characters total
      $lenone = $len - 5;

      // $colors is now only the values and the commas (",") that seperate them
      // rgb(51,102,153) is now 51,102,153 - we need this to explode the data in the next step
      $colors = substr($colors, 4, $lenone);

      // explode the color substring - should be in X,Y,Z format now.
      // $extras takes any characters more than the X,Y,Z format.
      list($r, $g, $b, $extras) = explode(",",$colors);

      // Check the rgb values that were just exploded.
      if($r < 0 or $r > 255 or $g < 0 or $g > 255 or $b < 0 or $b > 255){ $error++; }

      // Make sure that none of the values are blank  - prevents rgb(12) or rgb(12,12) from being valid
      if($r == "" or $g == "" or $b == ""){ $error++; }

      // \D in the character class means any non-decimal character except newline
      $rt = preg_match_all("[\D]", $r, $matches);
      $gt = preg_match_all("[\D]", $g, $matches);
      $bt = preg_match_all("[\D]", $b, $matches);

      // If we matched any characters above the values are not valid.
      // Also is there is anything in $extras - the whole color code is an invalid form!
      if($rt > 0 or $gt > 0 or $bt > 0 or $extras != ""){ $error++; }

      // If there are any errors then the color is not valid, return false, otherwise
      // we're okay and we retun true
      if($error > 0){ return false; }else{ return true; }
    }
  }else{
    echo "Error in function check_color(): Invalid argument for color type.";
    return false;
  }
}



WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com