      SUBROUTINE DFCLRRAMP(NSMCLRS,NPAT,IPAT)

C     * MAR 11/2004 - M.BERKLEY (MODIFIED for CCCMA)

      IMPLICIT NONE

      INTEGER NSMCLRS,NPAT
      INTEGER IPAT(NPAT)

C ************************************************************************
C
C  DESCRIPTION
C       THIS SUBROUTINE DEFINES THE GKS COLOUR PALETTES FOR A SMOOTH COLOUR
C       RAMP.  IT MUST BE PASSED THE NUMBER OF USER-DEFINED COLOURS AND THE
C       FIRST AND LAST COLOUR IN THE RAMP.
C  METHOD
C       ALL COLOURS ARE DEFINED USING THE HUE-SATURATION-VALUE METHOD
C       AND CONVERTED TO RGB VALUES USING 'HSVRGB'.  THESE VALUES ARE
C       THEN PASSED ON TO 'GSCR' WHICH DEFINES THE COLOUR OF THE PALETTE.
C       SINCE THE COLOUR INDEXES 0 AND 1 (BLACK AND WHITE RESPECTIVELY)
C       ARE USED TO DRAW THE BACKGROUND AND THE FOREGROUND, THE REDEFINED
C       PALETTES START AT 2.
C
C  ARGUMENTS
C       NCLRS  - THE NUMBER OF USER-DEFINED COLOURS.
C
C       ISTART - FIRST COLOUR.
C       IEND   - LAST COLOUR.
C
C  HISTORY
C       97/09/30  M. BERKLEY - REWORKED FROM DFCLRS CODE.
C
C *************************************************************************

      INTEGER IP,I,J,K
      INTEGER NSZCLR,NCLRS,ICURRCOL
      PARAMETER (NSZCLR = 255)
      REAL ZCLRS(3, 2:NSZCLR),ZBEG(3),ZEND(3),ZCURR(3)
      REAL RED, GREEN, BLUE

      DATA (ZCLRS(1, I), I=38, 41) / 4*0.0 /
      DATA (ZCLRS(2, I), I=38, 41) / 4*0.0 /
      DATA (ZCLRS(3, I), I=38, 41) / 4*0.0 /

CDEBUG
C!!!      WRITE(*,*) 'Start dfclrramp: NPAT: ',NPAT,'NSMCLRS: ',NSMCLRS
CDEBUG
C
C     * SET THE FOREGROUND COLOUR TO BLACK AND THE BACKGROUND COLOUR
C     * TO WHITE.
C
      CALL GSCR(1, 0, 1.0, 1.0, 1.0)
      CALL GSCR(1, 1, 0.0, 0.0, 0.0)

C
C     * DEFINE THE DEFAULT COLOURS, IN HUE, SATURATION AND VALUE.
C

C
C     * DEFINE THE FIRST 36 COLOURS - GO AROUND THE COLOUR WHEEL IN 10
C     * DEGREE INCREMENTS
C
      DO 100 I = 2, 37
           ZCLRS(1, I) = (I - 2)*10.0
           ZCLRS(2, I) = 1.0
           ZCLRS(3, I) = 1.0
  100 CONTINUE

C
C     * DEFINE COLOURS 42 TO 101.
C
      DO 110 I = 0, 9

C          * GREY SHADES
           ZCLRS(1, 42 + I) = 0.0
           ZCLRS(2, 42 + I) = 0.0

C          * RED SHADES
           ZCLRS(1, 52 + I) = 0.0

C          * GREEN SHADES
           ZCLRS(1, 62 + I) = 120.0

C          * BLUE SHADES
           ZCLRS(1, 72 + I) = 240.0

C          * CYAN SHADES
           ZCLRS(1, 82 + I) = 180.0

C          * MAGENTA SHADES
           ZCLRS(1, 92 + I) = 300.0

  110 CONTINUE

C
C     * DEFINE THE BRIGHTNESS VALUES FOR THE GREY SHADES.
C
      DO 160 I = 42, 51
           ZCLRS(3, I) = 1.0 - (I - 42)*0.1
  160 CONTINUE


C
C     * DEFINE THE SATURATION AND BRIGHTNESS VALUES FOR THE FIRST 6
C     * SHADES OF EACH OF THE ABOVE COLOURS.
C
      DO 120 I = 52, 92, 10
           DO 130 J = 0, 5
                ZCLRS(2, I + J) = 1.0 - (5 - J)*0.15
                ZCLRS(3, I + J) = 1.0
  130      CONTINUE
  120 CONTINUE

C
C     * DEFINE THE SATURATION AND BRIGHTNESS VALUES FOR THE LAST 4
C     * SHADES IN EACH OF THE ABOVE COLOURS.
C
      DO 140 I = 57, 97, 10
           DO 150 J = 1, 4
                ZCLRS(2, I + J) = 1.0
                ZCLRS(3, I + J) = 1.0 - J*0.125
  150      CONTINUE
  140 CONTINUE


C     * FORCE SOLID FILLS
      CALL GSFAIS(1)

C     * RECORD FIRST AND LAST COLOUR

      NCLRS=NSMCLRS/(NPAT-1)

      ICURRCOL=2
      DO IP=1,(NPAT-1)
         DO I=1,3
            ZBEG(I)=ZCLRS(I,IPAT(IP)-98)
         ENDDO
         DO I=1,3
            ZEND(I)=ZCLRS(I,IPAT(IP+1)-98)
         ENDDO
         
CDEBUG
C!!!         WRITE(*,*) 'NPAT: ',NPAT,'NSMCLRS: ',NSMCLRS,'NCLRS: ',NCLRS
C!!!         WRITE(*,*) 'CLRS: ',IPAT(IP),'(',IPAT(IP)-98,')',
C!!!     1        IPAT(IP+1),'(',IPAT(IP+1)-98,')'
C!!!         WRITE(*,*)'ZBEG: ',ZBEG
C!!!         WRITE(*,*)'ZEND: ',ZEND
CDEBUG
         
C     * ALLOCATE THE USER SPECIFIED COLOUR RAMP IN THE GKS COLOUR MAP.

         DO I = 0, NCLRS
            DO J=1,3
               ZCURR(J)=ZBEG(J)+
     1              I*(ZEND(J)-ZBEG(J))/NCLRS
            ENDDO
            CALL HSVRGB(ZCURR(1),
     +           ZCURR(2),
     +           ZCURR(3),
     +           RED, GREEN, BLUE)
 90         FORMAT(3(I4,1X),A1,1X,3(F7.3,1X),A3,3(F7.3,1X),A1)
CDEBUG
C!!!            WRITE(*,90)I,ICURRCOL,I+ICURRCOL,
C!!!     1           '(',ZCURR,') (',RED,GREEN,BLUE,')'
CDEBUG
            CALL GSCR(1, I+ICURRCOL, RED, GREEN, BLUE)
CDEBUG
C!!!            CALL GQCR(1,I+ICURRCOL,0,K,RED,GREEN,BLUE)
C!!!            CALL RGBHSV(RED,GREEN,BLUE,ZCURR(1),ZCURR(2),ZCURR(3))
C!!!            WRITE(*,90)I,ICURRCOL,I+ICURRCOL,
C!!!     1           '(',ZCURR,') (',RED,GREEN,BLUE,')'
CDEBUG
         ENDDO

         ICURRCOL=ICURRCOL+NCLRS+1
      ENDDO
C
C DONE.
C
      RETURN
C
      END
