summaryrefslogtreecommitdiff
path: root/ipl/gprogs/colrbook.icn
blob: 01313caaa5f4b949a438819f1d1792369870672c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
############################################################################
#
#	File:     colrbook.icn
#
#	Subject:  Program to show the named colors
#
#	Author:   Gregg M. Townsend
#
#	Date:     December 1, 1997
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#     colrbook is a mouse-driven program for choosing named colors.
#  Along the left are 24 equally spaced hues plus black, gray, white,
#  brown, violet, and pink.  Click on any of these to see the twenty
#  colors that are possible by adding lightness and saturation
#  modifiers to the particular hue.
#
############################################################################
#
#  Requires:  Version 9 graphics
#
############################################################################
#
#  Links: button, evmux, graphics
#
############################################################################

link button
link evmux
link graphics

$define BevelWidth 2
$define WindowMargin 10

$define HEADER 20	# height of header area (not incl. margin)
$define FOOTER 20	# height of footer area (not incl. margin)

$define TSIZ 12		# hue triangle size
$define HUEW 20		# hue width
$define HGAP 1		# hue gap

$define LEFT (m+TSIZ+HUEW+labw)	# total space to left of grid and its margin


global cwin, huelist, sats, lgts, colrs, fillargs
global labw, leftx, w, h, m

procedure main(args)
   local x, y, dx, dy, cw, ch
   local i, j, ij, hue, r

   lgts := ["pale", "light", "medium", "dark", "deep"]
   sats := ["weak", "moderate", "strong", "vivid"]
   colrs := table()
   fillargs := table()

   Window("size=500,350", "font=Helvetica,bold,12", args)
   cwin := Clone()
   m := WindowMargin
   w := WAttrib("width") - 2 * m
   h := WAttrib("height") - 2 * m
   labw := TextWidth("medium") + 3 * m	# label area width
   leftx := m + TSIZ + HUEW + labw	# space to left of grid and its margin

   dx := (w - leftx + m) / *sats
   dy := (h - HEADER - FOOTER + m) / *lgts
   cw := dx - m
   ch := dy - m

   inithues()

   every i := 1 to *sats do
      every j := 1 to *lgts do {
         ij := i || j
         x := leftx + dx * i - cw
         y := HEADER + dy * j - ch
         BevelRectangle(x, y, cw, ch, -BevelWidth)
         fillargs[ij] := [cwin, x + BevelWidth, y + BevelWidth,
            cw - 2 * BevelWidth, ch - 2 * BevelWidth]
         if Fg(cwin, colrs[ij] := NewColor("gray")) then	# may fail
            FillRectangle ! fillargs[ij]
         }
   every i := 1 to *sats do {
      GrooveRectangle(leftx + m + dx * (i - 1), m / 2, dx - m, HEADER)
      CenterString(leftx + dx * i - cw / 2, m / 2 + HEADER / 2, sats[i])
      }
   every j := 1 to *lgts do {
      GrooveRectangle(leftx, HEADER + dy*j - ch/2 - HEADER/2, -labw + m, HEADER)
      RightString(leftx - m, HEADER + dy*j - ch/2, lgts[j])
      }

   # define sensors
   button(&window, "QUIT", argless, exit, m+TSIZ+HUEW+m, m, labw-2*m, HEADER)
   sensor(&window, &lpress, hueclick, r, m, m, TSIZ + HUEW, h)
   quitsensor(&window)

   # initialize to "gray" hues using an artificial event
   Enqueue(&lrelease)
   hueclick(&window, 0, m, m + integer((*huelist - 4.5) / *huelist * h))

   # enter event loop
   evmux(&window)
end

procedure hueclick(win, arg, x, y)
   local hue, e, n, i, j

   e := &ldrag
   while e ~=== &lrelease do {
      if e === &ldrag then {
         n := (*huelist * (y - m + HGAP / 2)) / h + 1
         if 0 < n <= *huelist then {
            hue := huelist[n]
            EraseArea(m, m - TSIZ / 2, TSIZ + 1, h + TSIZ)
            y := m - HGAP + integer((n - 0.5) * (h + HGAP) / *huelist)
            BevelTriangle(m + TSIZ / 2, y, TSIZ / 2, "e")
            setcolor(hue)
            EraseArea(LEFT, m + h - FOOTER, w, FOOTER + m)
            CenterString(LEFT + (w - LEFT + m)/2, m + h + m/2 - FOOTER/2, hue)
            }
         }
      e := Event(win)
      y := &y
      }
   return
end

procedure setcolor(hue)
   local i, j, ij, prefix
   static prev

   every i := 1 to *sats do
      every j := 1 to *lgts do {
         ij := i || j
         prefix := lgts[j] || " " || sats[i] || " "
         if not Color(cwin, \colrs[ij], prefix || hue) then {
            # no mutable color was allocated;
            # free old static color, preserving grays (used for decoration)
            # also preserving labeling colors ("medium vivid")
            if \prev ~== "black" & \prev ~== "gray" & \prev ~== "white" then
               FreeColor(cwin, ("medium vivid " ~== prefix) || \prev)
            Fg(cwin, prefix || hue)
            FillRectangle ! fillargs[ij]
            }
         }

   prev := hue
   return
end

procedure inithues()
   local i, y1, y2, dy, win

   huelist := [
      "red", "orange", "red-yellow", "reddish yellow",
      "yellow", "greenish yellow", "yellow-green", "yellowish green",
      "green", "cyanish green", "cyan-green", "greenish cyan",
      "cyan", "bluish cyan", "blue-cyan", "cyanish blue",
      "blue", "blue-purple", "purple", "purple-magenta",
      "magenta", "reddish magenta", "magenta-red", "magentaish red",
      "black", "gray", "white",
      "brown", "violet", "pink"
      ]
   dy := real(h + HGAP) / *huelist
   win := Clone(&window)
   every i := 1 to *huelist do {
      y1 := integer(dy * (i - 1))
      y2 := integer(dy * i)
      Fg(win, huelist[i])
      FillRectangle(win, m + TSIZ + 1, m + y1, HUEW - 1, y2 - y1 - HGAP)
      }
   Uncouple(win)
   return
end