\cancel mode verify
! squeeze_colors.jnl  *sh* 4/94 for Ferret V3.10

! Description: Modify a palette by squeezing and stretching the color scale

say For detailed instructions examine the file  $FER_DIR/go/squeeze_colors.jnl

! Usage:   GO squeeze_colors old_lo old_hi new_lo new_hi [input_palette] [output_palette]
! defaults:  input_palette = default    output_palette = squeeze

! To enhance the effectiveness of color it is often desirable to concentrate
! color contrasts at certain ranges of the data being displayed.  The full-
! blown version of this concept is a histogram-based distortion of the color
! mapping (which could be developed with a FERRET script - see histogram.jnl).
! This script provides the more limited functionality: a range of the original
! color palette from old_lo to old_hi percent will be remapped to a new range
! extending from new_lo to new_hi.  For example, the middle 20% of the color
! scale could be expanded to 40% using 40,60 --> 30,70.  The total range of
! colors implied by the palette will be unchanged - only the distribution of
! this color range along the 0-100% scale will be effected.

! example: stretch the first 60 percent of colors into 80 percent of the scale
!	yes? set data coads_climatology
!	yes? shade/l=1/levels=(-5,35,1)/palette=rnb2 sst ! normal rnb2 palette
!	yes? GO squeeze_colors 40 60 30 70 rnb2 rnb2_shift
!	yes? shade/l=1/levels=(-5,35,1)/palette=rnb2_shift sst ! new palette

! check that all necessary input arguments were supplied
QUERY/IGNORE $1"<Usage:GO squeeze_colors orig_lo% orig_hi% new_lo% new_hi% [input_palette] [output_palette]"
QUERY/IGNORE $2"<Usage:GO squeeze_colors orig_lo% orig_hi% new_lo% new_hi% [input_palette] [output_palette]"
QUERY/IGNORE $3"<Usage:GO squeeze_colors orig_lo% orig_hi% new_lo% new_hi% [input_palette] [output_palette]"
QUERY/IGNORE $4"<Usage:GO squeeze_colors orig_lo% orig_hi% new_lo% new_hi% [input_palette] [output_palette]"

! save the users region and data set to be restored later
define region/default husr
set data/save

! initialize
cancel region/x
sp rm -f $6&squeeze&.spk    ! delete old output spectrum by this name

! read the specified input spectrum name (Default:$FER_PALETTE/default.spk)
! using its "percentage" variable as the data set axis
file/var=old_pcent,red,green,blue "$FER_PALETTE/$5&default&.spk"
define axis/from/x/name=axin old_pcent
define grid/x=axin  gold
cancel data/noerr $5&default&.spk
file/var=old_pcent,red,green,blue/grid=gold "$FER_PALETTE/$5&default&.spk"

! recompute percentages to associate with RGB triples
let/quiet new_pcent = IF old_pcent LT $1 THEN old_pcent*($3/$1) ELSE mid_pcent
let/quiet mid_pcent = IF old_pcent LT $2 THEN (old_pcent-$1)*(($4-$3)/($2-$1)) + $3 ELSE hi_perc
let/quiet hi_perc   = IF old_pcent LT 100 THEN (old_pcent-$2) * ((100-$4)/(100-$2)) + $4 ELSE 100

! write out the squeezed spectrum
! note: output precision restricted to 1 decimal place consistent with PLOT+ 
list/file=$6&squeeze&.spk/nohead/format=(4F10.1) new_pcent,red,green,blue

! wrap up
cancel data/noerr $5&default&.spk
set region husr
set data/restore
set mode/last verify
