#! /bin/sh
#+++
#  title:	cursor-color
#  abstract:	Load a colorized (customized) mouse cursor
#  author:	Tom Hageman, The Netherlands <tom@basil.icce.rug.nl>
#  created:	November 1996, from a PS hack posted by <bbum@friday.com>
#  modified:	
#  copyleft:
#
#		Copyright (C) 1996  Tom R. Hageman.
#		   (except the postscript hack)
#
#	This is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This software is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this software; see the file COPYING.  If not, write to
#	the Free Software Foundation, 59 Temple Place - Suite 330,
#	Boston, MA 02111-1307, USA.
#
#  description:
#
#	Warning: this uses undocumented features of the NEXTSTEP windowserver.
#	It may or may not work.  It even may crash the windowserver.
#	So, use it at your own risk.  
#
#  wishlist:
#	- allow arbitrary cursor shapes.
#	- support multiple bit depths.
#	- specify colors by name.
#---*/

version() {
  echo "`basename $0` version 0.3 -- November 1996"
}

usage() {
  echo "Usage: `basename $0` [-f|-r] [ interior-RGB[A] [ border-RGB[A] ] ]"
}

help()	{
  usage
  echo "\
  colors are given in hexadecimal RGB[A] brightness values.
  e.g. \"000\" means black, \"fff\" means white.
  Alpha (transparency) is optional, default is \"f\" i.e. fully opaque.
  Caveat: non-opaque cursors can give \"interesting\" effects.
  If no argument is given, a default (white-rimmed black) cursor is set.
options:
  -f, --fat         use fat cursor shape which should be more easily spotted
  -r, --rainbow     use rainbow interior (interior-color is ignored)
  -H, --help        show this help, and exit
  -V, --version     show version, and exit"
}

check_color() { # $1=variable-to-set $2=value $3=default
  _var="$1"
  case "$2" in
  [0-9a-f][0-9a-f][0-9a-f])
    _color=${2}f
    ;; #(
  [0-9a-f][0-9a-f][0-9a-f][1-9a-f])
    _color=${2}
    ;; #(
  "")
    _color=$3
    ;; #(
  *)
    echo "Invalid color value $2 -- must be hexadecimal RGB[A]" >&2
    usage >&2
    echo "(try \``basename $0` --help' for more information)" >&2
    exit 2
  esac
  eval "$_var=\$_color"  
}

type=normal

while :
do
  case $# in
  0) break
  esac
  case "$1" in #(
  -H | --help | --hel | --he | --h )
    help; exit 0
    ;; #(
  -V | --version | --versio | --versi | --vers | --ver | --ve | --v )
    version; exit 0
    ;; #(
  -f | --fat | --fa | -f )
    type=fat
    ;; #(
  -r | --rainbow | --rainbo | --rainb | --rain | --rai | --ra | --r )
    type=rainbow
    ;; #(
  --debug)
    debug=yes
    ;; #(
  -*)
    usage >&2; exit 2
    ;; #(
  *)
    break
  esac
  shift
done

case $# in #(
[012]) ;; #(
*) usage >&2; exit 2
esac

I="$1"
B="$2"
check_color I "$I" 000f
check_color B "$B" ffff

case $type in #(
normal)
  cursor="\
${B}${B}00000000000000000000000000000000000000000000000000000000
${B}${I}${B}0000000000000000000000000000000000000000000000000000
${B}${I}${I}${B}000000000000000000000000000000000000000000000000
${B}${I}${I}${I}${B}00000000000000000000000000000000000000000000
${B}${I}${I}${I}${I}${B}0000000000000000000000000000000000000000
${B}${I}${I}${I}${I}${I}${B}000000000000000000000000000000000000
${B}${I}${I}${I}${I}${I}${I}${B}00000000000000000000000000000000
${B}${I}${I}${I}${I}${I}${I}${I}${B}0000000000000000000000000000
${B}${I}${I}${I}${I}${I}${I}${I}${I}${B}000000000000000000000000
${B}${I}${I}${I}${I}${I}${B}${B}${B}${B}${B}00000000000000000000
${B}${I}${I}${B}${I}${I}${B}000000000000000000000000000000000000
${B}${I}${B}0000${B}${I}${I}${B}00000000000000000000000000000000
${B}${B}00000000${B}${I}${I}${B}00000000000000000000000000000000
${B}0000000000000000${B}${I}${I}${B}0000000000000000000000000000
00000000000000000000${B}${I}${I}${B}0000000000000000000000000000
000000000000000000000000${B}${B}00000000000000000000000000000000"
  ;;
fat)
  cursor="\
${B}${B}00000000000000000000000000000000000000000000000000000000
${B}${I}${B}${B}000000000000000000000000000000000000000000000000
${B}${I}${I}${I}${B}${B}0000000000000000000000000000000000000000
${B}${I}${I}${I}${I}${I}${B}${B}00000000000000000000000000000000
${B}${I}${I}${I}${I}${I}${I}${I}${B}${B}000000000000000000000000
${B}${I}${I}${I}${I}${I}${I}${I}${I}${I}${B}${B}0000000000000000
${B}${I}${I}${I}${I}${I}${I}${I}${I}${I}${I}${I}${B}${B}00000000
${B}${I}${I}${I}${I}${I}${I}${I}${I}${I}${I}${I}${I}${I}${B}${B}
${B}${I}${I}${I}${I}${I}${I}${I}${I}${I}${I}${B}${B}${B}00000000
${B}${I}${I}${I}${I}${I}${I}${I}${I}${B}${B}00000000000000000000
${B}${I}${I}${I}${I}${I}${I}${I}${I}${I}${B}00000000000000000000
${B}${I}${I}${I}${B}${I}${I}${I}${I}${I}${I}${B}0000000000000000
${B}${I}${I}${B}0000${B}${I}${I}${I}${I}${I}${I}${B}000000000000
${B}${I}${B}00000000${B}${I}${I}${I}${I}${B}${B}0000000000000000
${B}${B}0000000000000000${B}${I}${B}${B}000000000000000000000000
${B}00000000000000000000${B}${B}00000000000000000000000000000000"
  ;;
rainbow)
  cursor="\
${B}${B}00000000000000000000000000000000000000000000000000000000
${B}0d0f${B}0000000000000000000000000000000000000000000000000000
${B}0d0f5d0f${B}000000000000000000000000000000000000000000000000
${B}5d0fad0f5d0f${B}00000000000000000000000000000000000000000000
${B}ad0ffd0ffd0fad0f${B}0000000000000000000000000000000000000000
${B}fd0ffa0ffa0ffa0ffd0f${B}000000000000000000000000000000000000
${B}fa0ff50ff50ff50ff50ffa0f${B}00000000000000000000000000000000
${B}f50fd30fd30fd00fd00fd04f908f${B}0000000000000000000000000000
${B}d30fd00fd00fd04fd04fd04f908f908f${B}000000000000000000000000
${B}d04fd04fd04f908f908f${B}${B}${B}${B}${B}00000000000000000000
${B}908f908f${B}40df40df${B}000000000000000000000000000000000000
${B}908f${B}0000${B}40ff20ff${B}00000000000000000000000000000000
${B}${B}00000000${B}20ff00ff${B}00000000000000000000000000000000
${B}0000000000000000${B}00ff00bf${B}0000000000000000000000000000
00000000000000000000${B}00bf00bf${B}0000000000000000000000000000
000000000000000000000000${B}${B}00000000000000000000000000000000"
  ;;
esac

case "$debug" in
yes) echo interior=$I, border=$B
     echo "$cursor"
     exit 0
esac

pft << __EOF__ >/dev/null 2>&1
%!PS-Adobe-2.0 EPSF-2.0
%%BoundingBox: 0 0 16 16
%%EndComments
0 0 16 16 Retained window
dup windowdeviceround
gsave
16 16 scale
16 16 4
[16 0 0 -16 0 16]
{<
$cursor
>} false 3 alphaimage
grestore
gstate
nextdict /_NXSharedGrayAlpha get 
NX_TwelveBitRGB 1 index setwindowdepthlimit
windowdeviceround
0 0 16 16 5 4 roll 0 32 Copy composite
nulldevice
termwindow
__EOF__
