您的位置:程序门 -> vb -> 多媒体



如何把rgb颜色模式转换为hsv模式?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何把rgb颜色模式转换为hsv模式?[已结贴,结贴人:myrfy2004]
发表于:2007-05-27 17:41:56 楼主
如何把rgb颜色模式转换为hsv模式?谢谢。
这是我从vc的代码转换成的vb代码,是否正确?

private   function   rgb_to_hsv(r   as   byte,   g   as   byte,   b   as   byte,   h   as   byte,   s   as   byte,   v   as   byte)

        dim   max_v   as   byte,   min_v   as   byte
        dim   diff   as   single,   r_dist   as   single,   g_dist   as   single,   b_dist   as   single
        dim   hue   as   single
        dim   undefined         as   byte
        undefined   =   0
        h   =   0
        s   =   255
        v   =   255
       
        max_v   =   max_rgb(r,   g,   b)
        min_v   =   min_rgb(r,   g,   b)
        diff   =   max_v   -   min_v
        v   =   max_v
           
        if   max_v   <>   0   then
            s   =   (255#   *   diff   /   max_v)
        else
            s   =   0
        end   if
           
        if   (s   =   0)   then
            h   =   undefined
       
        else
            r_dist   =   (max_v   -   r)   /   diff
            g_dist   =   (max_v   -   g)   /   diff
            b_dist   =   (max_v   -   b)   /   diff
               
            if   (r   =   max_v)   then
                hue   =   b_dist   -   g_dist
            elseif   (g   =   max_v)   then
                hue   =   2   +   r_dist   -   b_dist
           
            elseif   (b   =   max_v)   then
                hue   =   4   +   g_dist   -   r_dist
            end   if
               
            if   (hue   <   0)   then
                hue   =   hue   +   6
            end   if
               
            h   =   (hue   *   (256#   /   6#))
        end   if
       

end   function

private   function   max_rgb(r   as   byte,   g   as   byte,   b   as   byte)   as   byte
if   r   >   g   and   r   >   b   then   max_rgb   =   r
if   g   >   r   and   g   >   b   then   max_rgb   =   g
if   b   >   r   and   b   >   g   then   max_rgb   =   b
end   function

private   function   min_rgb(r   as   byte,   g   as   byte,   b   as   byte)   as   byte
if   r   <   g   and   r   <   b   then   min_rgb   =   r
if   g   <   r   and   g   <   b   then   min_rgb   =   g
if   b   <   r   and   b   <   g   then   min_rgb   =   b
end   function

源c语言如下:
color::rgb_to_hsv(byte       r,       byte       g,       byte       b,       byte       &h,       byte       &s,       byte       &v)      
    {      
        byte       max_v,       min_v;      
        float       diff,       r_dist,       g_dist,       b_dist;      
        float       hue;      
        byte       undefined       =       0;      
       
        h       =       0;      
        s       =       255;      
        v       =       255;      
       
        max_v       =       max_rgb(r,g,b);      
        min_v       =       min_rgb(r,g,b);      
        diff       =       max_v       -       min_v;      
        v       =       max_v;      
           
        if(       max_v       !=       0       )       {      
            s       =       (byte)(255.0*diff/max_v);      
        }      
        else       {      
            s       =       0;      
        }      
           
        if(       s       ==       0       )       {      
            h       =       undefined;      
        }      
        else       {      
            r_dist       =       (max_v       -       r)/diff;      
            g_dist       =       (max_v       -       g)/diff;      
            b_dist       =       (max_v       -       b)/diff;      
               
            if(       r       ==       max_v       )       {      
                hue       =       b_dist       -       g_dist;      
            }      
            else       if(       g       ==       max_v       )       {      
                hue       =       2       +       r_dist       -       b_dist;      
            }      
            else       if(       b       ==       max_v       )       {      
                hue       =       4       +       g_dist       -       r_dist;      
            }      
               
            if(hue <0)       {      
                hue       +=       6.0;      
            }      
               
            h       =       (byte)(hue       *       (256.0/6.0));      
        }      
       
    /*****      
        float       h=0,s=1.0,v=1.0;      
        float       max_v,min_v,diff,r_dist,g_dist,b_dist;      
        float       undefined       =       0.0;      
       
        max_v       =       maxrgb(r,g,b);      
        min_v       =       minrgb(r,g,b);      
        diff       =       max_v       -       min_v;      
        v       =       max_v;      
           
        if(       max_v       !=       0       )      
            s       =       diff/max_v;      
        else      
            s       =       0.0;      
           
        if(       s       ==       0       )      
            h       =       undefined;      
        else       {      
            r_dist       =       (max_v       -       r)/diff;      
            g_dist       =       (max_v       -       g)/diff;      
            b_dist       =       (max_v       -       b)/diff;      
               
            if(       r       ==       max_v       )          
                h       =       b_dist       -       g_dist;      
            else       if(       g       ==       max_v       )      
                h       =       2       +       r_dist       -       b_dist;      
            else       if(       b       ==       max_v       )      
                h       =       4       +       g_dist       -       r_dist;      
            else      
                printf( "rgb2hsv::how       did       i       get       here?\n ");      
            h       *=       60;      
            if(       h       <       0)      
                h       +=       360.0;      
        }      
发表于:2007-05-27 18:19:171楼 得分:20
未测试:

option   explicit

      '~~~rgb   color   properties
      public   red   as   integer
      public   green   as   integer
      public   blue   as   integer

      '~~~hsv   color   properties
      public   hue   as   single
      public   saturation   as   single
      public   value   as   single

      '~~~converts   rgb   to   hsv
      public   sub   tohsv()
              dim   fred   as   single
              dim   fgreen   as   single
              dim   fblue   as   single
              dim   fmx   as   single
              dim   fmn   as   single
              dim   fva   as   single
              dim   fsa   as   single
              dim   frc   as   single
              dim   fgc   as   single
              dim   fbc   as   single
              fred   =   red   /   255
              fgreen   =   green   /   255
              fblue   =   blue   /   255
              fmx   =   fred
              if   fgreen   >   fmx   then   fmx   =   fgreen
              if   fblue   >   fmx   then   fmx   =   fblue
              fmn   =   fred
              if   fgreen   <   fmn   then   fmn   =   fgreen
              if   fblue   <   fmn   then   fmn   =   fblue
              fva   =   fmx
              if   fmx   then
                      fsa   =   (fmx   -   fmn)   /   fmx
              else
                      fsa   =   0
              end   if
              if   fsa   =   0   then
                      hue   =   0
              else
                      frc   =   (fmx   -   fred)   /   (fmx   -   fmn)
                      fgc   =   (fmx   -   fgreen)   /   (fmx   -   fmn)
                      fbc   =   (fmx   -   fblue)   /   (fmx   -   fmn)
                      select   case   fmx
                      case   fred
                              hue   =   fbc   -   fgc
                      case   fgreen
                              hue   =   2   +   frc   -   fbc
                      case   fblue
                              hue   =   4   +   fgc   -   frc
                      end   select
                      hue   =   hue   *   60
                      if   hue   <   0   then   hue   =   hue   +   360
              end   if
              saturation   =   fsa   *   100
              value   =   fva   *   100
      end   sub

      '~~~converts   hsv   to   rgb
      public   sub   torgb()
              dim   fsaturation   as   single
              dim   fvalue   as   single
              dim   fhue   as   single
              dim   ni   as   integer
              dim   ff   as   single
              dim   fp   as   single
              dim   fq   as   single
              dim   ft   as   single
              dim   fred   as   single
              dim   fgreen   as   single
              dim   fblue   as   single
              fsaturation   =   saturation   /   100
              fvalue   =   value   /   100
              if   saturation   =   0   then
                      fred   =   fvalue
                      fgreen   =   fvalue
                      fblue   =   fvalue
              else
                      fhue   =   hue   /   60
                      if   fhue   =   6   then   fhue   =   0
                      ni   =   int(fhue)
                      ff   =   fhue   -   ni
                      fp   =   fvalue   *   (1   -   fsaturation)
                      fq   =   fvalue   *   (1   -   (fsaturation   *   ff))
                      ft   =   fvalue   *   (1   -   (fsaturation   *   (1   -   ff)))
                      select   case   ni
                      case   0
                              fred   =   fvalue
                              fgreen   =   ft
                              fblue   =   fp
                      case   1
                              fred   =   fq
                              fgreen   =   fvalue
                              fblue   =   fp
                      case   2
                              fred   =   fp
                              fgreen   =   fvalue
                              fblue   =   ft
                      case   3
                              fred   =   fp
                              fgreen   =   fq
                              fblue   =   fvalue
                      case   4
                              fred   =   ft
                              fgreen   =   fp
                              fblue   =   fvalue
                      case   5
                              fred   =   fvalue
                              fgreen   =   fp
                              fblue   =   fq
                      end   select
              end   if
              red   =   int(255.9999   *   fred)
              green   =   int(255.9999   *   fgreen)
              blue   =   int(255.9999   *   fblue)
end   sub


快速检索

最新资讯
热门点击