Results 1 to 2 of 2

Thread: rgb to hex conversion

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 1999
    Posts
    309

    rgb to hex conversion

    Anybody got a good rgb to hex conversion routine in ASP or javascript ?

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    This isnt the most efficient code but it does the job.

    Code:
    function Num2Hex(n){
    
    if (n==0) return "0";
    if (n==1) return "1";
    if (n==2) return "2";
    if (n==3) return "3";
    if (n==4) return "4";
    if (n==5) return "5";
    if (n==6) return "6";
    if (n==7) return "7";
    if (n==8) return "8";
    if (n==9) return "9";
    if (n==10) return "A";
    if (n==11) return "B";
    if (n==12) return "C";
    if (n==13) return "D";
    if (n==14) return "E";
    if (n==15) return "F";
    
    }
    
    function rgb2Hex(r,g,b) {
    
    var r1,r2,g1,g2,b1,b2
    
    if (r>15)
    r1=Num2Hex(Math.floor(r/16));                                                    
    else
    r1=Num2Hex(0);
    
    r2=Num2Hex(r-16*Math.floor(r/16));
    
    if (b>15)
    b1=Num2Hex(Math.floor(b/16));                                                    
    else
    b1=Num2Hex(0);
    
    b2=Num2Hex(b-16*Math.floor(b/16));
    
    if (g>15)
    g1=Num2Hex(Math.floor(g/16));                                                    
    else
    g1=Num2Hex(0);
    
    g2=Num2Hex(g-16*Math.floor(g/16));
    
    
    
    
    return "#"+r1+r2+g1+g2+b1+b2;
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width