[GRASS-SVN] r31134 - grass-addons/raster/r.colors.tools

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Apr 27 03:41:52 EDT 2008


Author: hamish
Date: 2008-04-27 03:41:52 -0400 (Sun, 27 Apr 2008)
New Revision: 31134

Modified:
   grass-addons/raster/r.colors.tools/r.cpt2grass
Log:
update to translate HSV input rules

Modified: grass-addons/raster/r.colors.tools/r.cpt2grass
===================================================================
--- grass-addons/raster/r.colors.tools/r.cpt2grass	2008-04-27 05:26:05 UTC (rev 31133)
+++ grass-addons/raster/r.colors.tools/r.cpt2grass	2008-04-27 07:41:52 UTC (rev 31134)
@@ -67,12 +67,21 @@
     exit 1
 fi
 
-if [ `grep 'COLOR_MODEL' "$GIS_OPT_INPUT" | cut -f2 -d= | grep -c 'HSV\|CMYK'` -gt 0 ] ; then
-    echo "ERROR: Only the RGB color model is supported" 1>&2
-    exit 1
-fi
+COLOR_MODEL=`grep '^# \?COLOR_MODEL' "$GIS_OPT_INPUT" | cut -f2 -d= | awk '{print $1}'`
 
+case "$COLOR_MODEL" in
+   RGB | HSV)
+     if [ -n "$GRASS_VERBOSE" ] && [ "$GRASS_VERBOSE" -gt 1 ] ; then
+        echo "Color model is $COLOR_MODEL" 1>&2
+     fi
+     ;;
+   CMYK | *)
+     echo "ERROR: Only the RGB and HSV color models are supported" 1>&2
+     exit 1
+     ;;
+esac
 
+
 # check if we have awk
 AWK=`which awk`
 if [ "$AWK" = "" ] ; then
@@ -91,7 +100,77 @@
 fi
 
 
+#### fn to convert hsv to rgb
+# Converts HSV to RGB. Based on the Foley and Van Dam HSV algorithm used
+#  in the hsv.rgb.sh script in GRASS 4/5 by James Westervelt (CERL)
+HSVtoRGB() {
+   # Hue: 0-360 degrees
+   H=$1
+   # Satuaration: 0.0-1.0
+   S=$2
+   # Value: 0.0-1.0
+   V=$3
 
+   #if V == 0: black
+   #if V == 1: white
+   #else chromatic calculation
+
+   awk -v H="$H" -v S="$S" -v V="$V" 'BEGIN {
+#     if(V == 0.0) {printf("R=0\nG=0\nB=0\n"); exit}
+#     if(V == 1.0) {printf("R=255\nG=255\nB=255\n"); exit}
+
+     if(V == 0.0) {printf("black\n"); exit}
+     if(V == 1.0) {printf("white\n"); exit}
+  
+     #print "chromatic: H=" H " S=" S " V=" V
+   
+     if(H >=360) {H -= 360}
+     h = H/60.0
+     i = int(h)
+     f = h-i
+     p = V * (1-S)
+     q = V * (1-S*f)
+     t = V * (1-S*(1-f))
+   
+     # init/fallback
+     R = G = B = 0
+     
+     #switch/case not portable in (g)awk?
+
+     # red
+     if(i==0) {R = V}
+     if(i==1) {R = q}
+     if(i==2) {R = p}
+     if(i==3) {R = p}
+     if(i==4) {R = t}
+     if(i==5) {R = V}
+   
+     # green
+     if(i==0) {G = t}
+     if(i==1) {G = V}
+     if(i==2) {G = V}
+     if(i==3) {G = q}
+     if(i==4) {G = p}
+     if(i==5) {G = p}
+   
+     # blue
+     if(i==0) {B = p}
+     if(i==1) {B = p}
+     if(i==2) {B = t}
+     if(i==3) {B = V}
+     if(i==4) {B = V}
+     if(i==5) {B = q}
+   
+      #printf("s=%.2f  h=%.2f i=%d  f=%.4f  p=%.4f  q=%.4f  t=%.4f\n", S, h, i, f, p, q, t)
+   
+#      printf("R=%d\nG=%d\nB=%d\n", R*255, G*255, B*255)
+      printf("%d:%d:%d\n", R*255, G*255, B*255)
+   }'
+}
+
+
+#### process cpt file
+
 # B background is for values less than the first color rule, F foreground is for greater.
 #  in GRASS we just consider that to be null, although we could define a color rule
 #  max<->near infinity to be solidly that color. ??
@@ -101,9 +180,20 @@
 echo "#  from [$GIS_OPT_INPUT] by $USER at `date`" >> "$TMP"
 grep '^#' "$GIS_OPT_INPUT" >> "$TMP"
 
+
 # null value color
-grep '^N' "$GIS_OPT_INPUT" | \
-  awk '{printf("nv %d:%d:%d\n", $2, $3, $4)}' >> "$TMP"
+case "$COLOR_MODEL" in
+   RGB)
+     grep '^N' "$GIS_OPT_INPUT" | \
+       awk '{printf("nv %d:%d:%d\n", $2, $3, $4)}' >> "$TMP"
+     ;;
+   HSV)
+     HSV=`grep '^N' "$GIS_OPT_INPUT" | awk '{print $2 " " $3 " " $4}'`
+     RGB=`HSVtoRGB $HSV`
+     #echo "HSV=[$HSV]  RGB=[$RGB]"
+     echo "nv $RGB" >> "$TMP"
+     ;;
+esac
 
 
 # do it in an awk script:
@@ -112,9 +202,35 @@
 #if N nv
 
 if [ $GIS_FLAG_S -eq 0 ] ; then
-  # the data
-  grep '^[0-9.-]' "$GIS_OPT_INPUT" | \
-    awk '{printf("%s %d:%d:%d\n%s %d:%d:%d\n", $1, $2, $3, $4, $5, $6, $7, $8)}' >> "$TMP"
+   # the data
+   case "$COLOR_MODEL" in
+      RGB)
+       grep '^[0-9.-]' "$GIS_OPT_INPUT" | \
+         awk '{printf("%s %d:%d:%d\n%s %d:%d:%d\n", $1, $2, $3, $4, $5, $6, $7, $8)}' >> "$TMP"
+       ;;
+      HSV)
+       while read LINE ; do
+          if [ `echo "$LINE" | grep -c '^[0-9.-]'` -lt 1 ] ; then
+	     continue
+	  fi
+          ELEV1=`echo "$LINE" | awk '{print $1}'`
+          H1=`echo "$LINE" | awk '{print $2}'`
+          S1=`echo "$LINE" | awk '{print $3}'`
+          V1=`echo "$LINE" | awk '{print $4}'`
+          ELEV2=`echo "$LINE" | awk '{print $5}'`
+          H2=`echo "$LINE" | awk '{print $6}'`
+          S2=`echo "$LINE" | awk '{print $7}'`
+          V2=`echo "$LINE" | awk '{print $8}'`
+
+          RGB1=`HSVtoRGB "$H1" "$S1" "$V1"`
+	  RGB2=`HSVtoRGB "$H2" "$S2" "$V2"`
+
+          echo "$ELEV1 $RGB1" >> "$TMP"
+	  echo "$ELEV2 $RGB2" >> "$TMP"
+       done < "$GIS_OPT_INPUT"
+       ;;
+   esac
+
 else
   # strech to fit data
 
@@ -143,16 +259,37 @@
      PERCENT_MAX=`echo "$LINE_MAX" | awk -v RANGE="$RANGE" -v CMAX="$CPT_MAX" \
         '{print 100 * (RANGE - (CMAX - $1)) / RANGE }'`
 
-     echo "$LINE" | awk -v PMIN="$PERCENT_MIN" -v PMAX="$PERCENT_MAX" \
-        '{printf("%s%% %d:%d:%d\n%s%% %d:%d:%d\n", PMIN, $2, $3, $4, PMAX, $6, $7, $8)}' >> "$TMP"
-
 # DEBUG:
 #echo "LINE: [$LINE]"
 #echo "  lmin=$LINE_MIN  lmax=$LINE_MAX  pmin=$PERCENT_MIN  pmax=$PERCENT_MAX "
 
+     case "$COLOR_MODEL" in
+       RGB)
+        echo "$LINE" | awk -v PMIN="$PERCENT_MIN" -v PMAX="$PERCENT_MAX" \
+          '{printf("%s%% %d:%d:%d\n%s%% %d:%d:%d\n", PMIN, $2, $3, $4, PMAX, $6, $7, $8)}' >> "$TMP"
+        ;;
+       HSV)
+        H1=`echo "$LINE" | awk '{print $2}'`
+        S1=`echo "$LINE" | awk '{print $3}'`
+        V1=`echo "$LINE" | awk '{print $4}'`
+        H2=`echo "$LINE" | awk '{print $6}'`
+        S2=`echo "$LINE" | awk '{print $7}'`
+        V2=`echo "$LINE" | awk '{print $8}'`
+
+        RGB1=`HSVtoRGB "$H1" "$S1" "$V1"`
+	RGB2=`HSVtoRGB "$H2" "$S2" "$V2"`
+
+        echo "${PERCENT_MIN}% $RGB1" >> "$TMP"
+	echo "${PERCENT_MAX}% $RGB2" >> "$TMP"
+        ;;
+     esac
+
   done < "$GIS_OPT_INPUT"
 fi
 
+
+#### apply new rules
+
 uniq "$TMP" "${TMP}.u"
 
 if [ -n "$GIS_OPT_MAP" ] ; then



More information about the grass-commit mailing list