Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > Flash In Action > Widgets

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-12-2004, 07:53 PM   #1
petefs
Fried Onions
 
petefs's Avatar
 
Join Date: Apr 2004
Location: Chicago, IL
Posts: 1,328
Send a message via AIM to petefs Send a message via MSN to petefs Send a message via Yahoo to petefs
Default PHP RGB<->HSV functions

had to write this yesterday, and hell... nobody else has posted here yet : )

dunno if it could be optimized. I wasn't able to turn anything up in a search, so here ya go if anybody wants it:

PHP Code:
<?php
function RGB_TO_HSV ($R$G$B)  // RGB Values:Number 0-255
{                                 // HSV Results:Number 0-1
   
$HSL = array();

   
$var_R = ($R 255);
   
$var_G = ($G 255);
   
$var_B = ($B 255);

   
$var_Min min($var_R$var_G$var_B);
   
$var_Max max($var_R$var_G$var_B);
   
$del_Max $var_Max $var_Min;

   
$V $var_Max;

   if (
$del_Max == 0)
   {
      
$H 0;
      
$S 0;
   }
   else
   {
      
$S $del_Max $var_Max;

      
$del_R = ( ( ( $max $var_R ) / ) + ( $del_Max ) ) / $del_Max;
      
$del_G = ( ( ( $max $var_G ) / ) + ( $del_Max ) ) / $del_Max;
      
$del_B = ( ( ( $max $var_B ) / ) + ( $del_Max ) ) / $del_Max;

      if      (
$var_R == $var_Max$H $del_B $del_G;
      else if (
$var_G == $var_Max$H = ( ) + $del_R $del_B;
      else if (
$var_B == $var_Max$H = ( ) + $del_G $del_R;

      if (
H<0$H++;
      if (
H>1$H--;
   }

   
$HSL['H'] = $H;
   
$HSL['S'] = $S;
   
$HSL['V'] = $V;

   return 
$HSL;
}

function 
HSV_TO_RGB ($H$S$V)  // HSV Values:Number 0-1
{                                 // RGB Results:Number 0-255
    
$RGB = array();

    if(
$S == 0)
    {
        
$R $G $B $V 255;
    }
    else
    {
        
$var_H $H 6;
        
$var_i floor$var_H );
        
$var_1 $V * ( $S );
        
$var_2 $V * ( $S * ( $var_H $var_i ) );
        
$var_3 $V * ( $S * (- ( $var_H $var_i ) ) );

        if       (
$var_i == 0) { $var_R $V     $var_G $var_3  $var_B $var_1 ; }
        else if  (
$var_i == 1) { $var_R $var_2 $var_G $V      $var_B $var_1 ; }
        else if  (
$var_i == 2) { $var_R $var_1 $var_G $V      $var_B $var_3 ; }
        else if  (
$var_i == 3) { $var_R $var_1 $var_G $var_2  $var_B $V     ; }
        else if  (
$var_i == 4) { $var_R $var_3 $var_G $var_1  $var_B $V     ; }
        else                   { 
$var_R $V     $var_G $var_1  $var_B $var_2 ; }

        
$R $var_R 255;
        
$G $var_G 255;
        
$B $var_B 255;
    }

    
$RGB['R'] = $R;
    
$RGB['G'] = $G;
    
$RGB['B'] = $B;

    return 
$RGB;
}
?>
petefs is offline   Reply With Quote
Old 07-12-2004, 08:14 PM   #2
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,758
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

Im pretty sure there are some on proto.layer51

Also, its not uncommon to use readily available JS implementations

But yours looks just as good as any of the others as far as I can tell.
__________________
(6)
senocular is offline   Reply With Quote
Old 07-12-2004, 08:28 PM   #3
petefs
Fried Onions
 
petefs's Avatar
 
Join Date: Apr 2004
Location: Chicago, IL
Posts: 1,328
Send a message via AIM to petefs Send a message via MSN to petefs Send a message via Yahoo to petefs
Default

bah, it figures. I should have looked there first : ) Plain as day.

You'd think for the five thousand times I've read through all the code available there that I would have remembered ^_^

Well, reinventing the wheel is part of learning ; )

Thanks ^_^
petefs is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:40 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.