POV-Ray : Newsgroups : povray.binaries.images : texture interpolation... why rgb ? : Re: texture interpolation... why rgb ? Server Time
2 Aug 2024 10:20:46 EDT (-0400)
  Re: texture interpolation... why rgb ?  
From: Trevor G Quayle
Date: 30 Nov 2007 12:45:00
Message: <web.47504bf52088bdecc150d4c10@news.povray.org>
Le Forgeron <jgr### [at] freefr> wrote:
> I would agree... would you mind sharing your SDL interpolation code ?

I don't mind.  It can be likely far better optimized.  It is quite easy to do as
a macro using #if, #range, etc., but was quite cumbersome to make a continuous
function through creative use of select(), especially since functions don't
handle vectors, so each component has to be split out separately and
recombined.

I have also influded the function for converting HSV/HSL to XYZ and back

The formulae for these are based on the ones from wikipedia
http://en.wikipedia.org/wiki/HSV_color_space

Let me know if you see any errors or have questions.

//RGB to HSV
#declare HHSV=function(R,G,B)
{
  60*
  select(
    min(R,G,B)-max(R,G,B)
  ,//min<max
    select(
      R-max(R,G,B)
    ,//R<max
      select(
        B-max(R,G,B)
      ,//G=max
        (B-R)/(max(R,G,B)-min(R,G,B))+2
      ,//B=max
        (R-G)/(max(R,G,B)-min(R,G,B))+4
      )
    ,//R=max
      (G-B)/(max(R,G,B)-min(R,G,B))+
      select(
        G-B
      ,//G>B
        6
      ,//B>G
        0
      )
    )
  ,//min=max
    0
  )
}
#declare SHSV=function(R,G,B) {select(-max(R,G,B),1-min(R,G,B)/max(R,G,B),0)}
#declare VHSV=function(R,G,B) {max(R,G,B)}
//END RGB to HSV

//RGB to HSL
#declare HHSL=function(R,G,B) {HHSV(R,G,B)}   //same as HSV
#declare SHSL=function(R,G,B)
{
  select(
    min(R,G,B)-max(R,G,B)
  ,//max>min
    (max(R,G,B)-min(R,G,B))/
    select(
      1-(max(R,G,B)+min(R,G,B))
    ,//l>1/2
      2-(max(R,G,B)+min(R,G,B))
    ,//1/2=>l
      max(R,G,B)+min(R,G,B)
    )
  ,//max=min
    0
  )
}
#declare LHSL=function(R,G,B){(max(R,G,B)+min(R,G,B))/2}
//END RGB to HSL

//HSL to RGB
#declare tHSL=function(H,C){(H/360+C/3)-floor(H/360+C/3)}
#declare qHSL=function(H,S,L){select(L-1/2,L*(1+S),L+S-L*S)}
#declare pHSL=function(H,S,L){2*L-qHSL(H,S,L)}
#declare CHSL=function(H,S,L,C)
{
  select(
    -S
  ,//S>0
    select(
      tHSL(H,C)-1/6
    ,//tc<1/6
      pHSL(H,S,L)+2*(qHSL(H,S,L)-L)*6*tHSL(H,C)
    ,
      select(
        tHSL(H,C)-1/2
      ,//tc<1/2
        qHSL(H,S,L)
      ,
        select(
          tHSL(H,C)-2/3
        ,//tc<2/3
          pHSL(H,S,L)+2*(qHSL(H,S,L)-L)*6*(2/3-tHSL(H,C))
        ,//esle
          pHSL(H,S,L)
        )
      )
    )
  ,//S=0
    L
  )
}
#declare RHSL=function (H,S,L){CHSL(H,S,L,+1)}
#declare GHSL=function (H,S,L){CHSL(H,S,L, 0)}
#declare BHSL=function (H,S,L){CHSL(H,S,L,-1)}
//END HSL to RGB




//HSV to RGB
#declare hHSV=function (H){floor(mod(H/60,6))}
#declare fHSV=function (H){H/60-hHSV(H)}
#declare pHSV=function (H,S,V){V*(1-S)}
#declare qHSV=function (H,S,V){V*(1-fHSV(H)*S)}
#declare tHSV=function (H,S,V){V*(1-(1-fHSV(H))*S)}
#declare RHSV=function (H,S,V)
{
  select(hHSV(H)-0.5
  ,//0
    V
  ,
    select(hHSV(H)-1.5
    ,//1
      qHSV(H,S,V)
    ,
      select(hHSV(H)-2.5
      ,//2
        pHSV(H,S,V)
      ,
        select(hHSV(H)-3.5
        ,//3
          pHSV(H,S,V)
        ,
          select(hHSV(H)-4.5
          ,//4
            tHSV(H,S,V)
          ,//5
            V
          )
        )
      )
    )
  )
}

#declare GHSV=function (H,S,V)
{
  select(hHSV(H)-0.5
  ,//0
    tHSV(H,S,V)
  ,
    select(hHSV(H)-1.5
    ,//1
      V
    ,
      select(hHSV(H)-2.5
      ,//2
        V
      ,
        select(hHSV(H)-3.5
        ,//3
          qHSV(H,S,V)
        ,//4,5
          pHSV(H,S,V)
        )
      )
    )
  )
}

#declare BHSV=function (H,S,V)
{
  select(hHSV(H)-0.5
  ,//0
    pHSV(H,S,V)
  ,
    select(hHSV(H)-1.5
    ,//1
      pHSV(H,S,V)
    ,
      select(hHSV(H)-2.5
      ,//2
        tHSV(H,S,V)
      ,
        select(hHSV(H)-3.5
        ,//3
          V
        ,
          select(hHSV(H)-4.5
          ,//4
            V
          ,//5
            qHSV(H,S,V)
          )
        )
      )
    )
  )
}
//END HSV to RGB


//CONVERT HSV to XYZ (polar to caresian) for interpolation and back
#declare HSVX=function(H,S,V){sin(radians(H))*S}
#declare HSVY=function(H,S,V){cos(radians(H))*S}
#declare HSVZ=function(H,S,V){V}
#declare
XYZH=function(X,Y,Z){select(-sqrt(X*X+Y*Y),abs(degrees(acos(Y/sqrt(X*X+Y*Y)))-select(X,360,0)),0)}
#declare XYZS=function(X,Y,Z){sqrt(X*X+Y*Y)}
#declare XYZV=function(X,Y,Z){Z}

#declare HSLX=function(H,S,L){sin(radians(H))*S}
#declare HSLY=function(H,S,L){cos(radians(H))*S}
#declare HSLZ=function(H,S,L){L}
//H & S same as above
#declare XYZL=function(X,Y,Z){Z}
//END CONVERT HSV to XYZ

#declare INTRGB=function (PTA,PTB,K){PTA+(PTB-PTA)*K}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.