Re: HSV color wheel

From: Mary Haley (haley AT XXXXXX)
Date: Tue Mar 23 2004 - 09:56:25 MST

  • Next message: Jonathan Vigh: "weird strippling/streaking pattern in a map"

    >
    > Hi,
    >
    > I am using the hsv2rgb.ncl script to create a color wheel for use in
    > some plots. I would to draw an actual color wheel to go with it like
    > those at
    >
    > http://ngwww.ucar.edu/ngdoc/ng/ug/ncl/gsun/basics.html#CreatingColorMapHSV
    >
    > is the script which drew these color wheels available?
    >
    > cheers,
    > jason
    >
    > --
    > Jason Evans Ph:(203)432-3440
    > Dept. of Geology and Geophysics fax:(203)432-3134
    > Yale University email:jason.evans AT yale.edu
    > PO Box 208109
    > New Haven, CT 06520-8109
    >

    Hi Jason,

    Below is the NCL that generates a color wheel very close to the one
    you see on the page above. (The color wheel on that page was generated
    by a Fortran program.) It's not very efficient because it has
    multiple nested do loops. I just basically converted the Fortran
    program almost verbatim.

    Note that I found a minor bug in "hsv2rgb" that causes the input
    values to be changed inside the hsv2rgb.ncl script. To fix this
    bug, I edited $NCARG_ROOT/lib/ncarg/nclex/gsun/gsn_code.ncl
    and changed the two lines:

    function hsv2rgb (h[*]:float,s[*]:float,v[*]:float)
    begin

    to the following five lines:
            
    function hsv2rgb (h_old[*]:float,s_old[*]:float,v_old[*]:float)
    begin
      h = h_old
      s = s_old
      v = v_old

    Once you have this fix, you can run the "cwheel.ncl" example:

    load "$NCARG_ROOT/lib/ncarg/nclex/gsun/gsn_code.ncl"

    begin
    ;
    ; Define the number of values, the number of hues, the number
    ; of saturations, and the number of points bounding a color box.
    ;
      nw = 3
      ncol = 16
      nsat = 4
      npts = 4
      x = new(npts,float)
      y = new(npts,float)

      pi = 3.14159265
      val = (/ 0.60, 0.80, 1.00 /)
    ;
    ; Y-coordinates for the saturation labels.
    ;
      ysat_pos = (/0.37, 0.285, 0.20/)

    ;
    ; Open workstation.
    ;
      wks = gsn_open_wks("x11","cwheel")

    ;
    ; Create variables to hold resource lists.
    ;
      poly_res = True
      text_res = True
      text_res@txFont = "helvetica"

    ;
    ; Loop on the values.
    ;
      rinc = 2 * pi/ncol
      hinc = 360.0/ncol
      sinc = 1.00/(nsat-1)

    ;
    ; One frame per "value" value.
    ;
      do il = 0,nw-1
    ;
    ; Loop on the hues.
    ;
        do ihue = 0,ncol-1
          hue = ihue * hinc
          theta1 = (ihue -.5) * rinc
          theta2 = (ihue +.5) * rinc
          x(0) = 0.0
          x(3) = 0.0
          y(0) = 0.0
          y(3) = 0.0
    ;
    ; Loop on the saturation values.
    ;
          do isat = 1,nsat
            sat = ((isat - 1) * sinc)
            rgb = hsv2rgb(hue,sat,val(il))
            poly_res@gsFillColor = NhlNewColor(wks,rgb(0,0),rgb(0,1),rgb(0,2))
            rlen = (1.*isat)/(1.*nsat)
            x(1) = cos(theta1) * rlen
            y(1) = sin(theta1) * rlen
            x(2) = cos(theta2) * rlen
            y(2) = sin(theta2) * rlen
            xndc = (x + 1.4)/2.8
            yndc = (y + 1.4)/2.8
            gsn_polygon_ndc(wks,xndc,yndc,poly_res)
            x(0) = x(1)
            x(3) = x(2)
            y(0) = y(1)
            y(3) = y(2)
    ;
    ; Saturation title.
    ;
            if(isat.ne.1) then
              sat_title = sprintf("S = %4.2f",sat)
              text_res@txJust = "CenterCenter"
              text_res@txFontHeightF = 0.02
              gsn_text_ndc(wks,sat_title,0.5,ysat_pos(isat-2),text_res)
            end if
          end do
        end do
    ;
    ; Main (value) title.
    ;
        main_title = sprintf("Value = %4.2f", val(il))
        text_res@txJust = "CenterCenter"
        text_res@txFontHeightF = 0.025
        gsn_text_ndc(wks,main_title,0.5,0.9,text_res)

    ;
    ; Hue titles.
    ;
        text_res@txFontHeightF = 0.02
        hue_title = "Hue = 0."
        text_res@txJust = "CenterLeft"
        gsn_text_ndc(wks,hue_title,0.87,0.5,text_res)

        hue_title = "Hue = 45."
        gsn_text_ndc(wks,hue_title,0.77,0.75,text_res)

        hue_title = "Hue = 135."
        text_res@txJust = "CenterRight"
        gsn_text_ndc(wks,hue_title,0.23,0.75,text_res)

        hue_title = "Hue = 225."
        gsn_text_ndc(wks,hue_title,0.23,0.25,text_res)

        text_res@txJust = "CenterLeft"
        hue_title = "Hue = 315."
        gsn_text_ndc(wks,hue_title,0.77,0.25,text_res)

    ;
    ; Advance the frame.
    ;
        frame(wks)
      end do
    end

    _______________________________________________
    ncl-talk mailing list
    ncl-talk AT ucar.edu
    http://mailman.ucar.edu/mailman/listinfo/ncl-talk



    This archive was generated by hypermail 2b29 : Tue Mar 23 2004 - 09:58:46 MST