summaryrefslogtreecommitdiffstats
path: root/tests/color.test
blob: b7fed15dbaf9c477fb47ed243f7481599faf75c5 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# This file is a Tcl script to test out the procedures in the file
# tkColor.c.  It is organized in the standard fashion for Tcl tests.
#
# Copyright (c) 1995-1998 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
# All rights reserved.
#
# RCS: @(#) $Id: color.test,v 1.5 2000/11/02 01:18:35 hobbs Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    source [file join [pwd] [file dirname [info script]] defs.tcl]
}

if {[info commands testcolor] != "testcolor"} {
    puts "testcolor command not available; skipping tests"
    ::tcltest::cleanupTests
    return
}

eval destroy [winfo children .]
wm geometry . {}
raise .

# cname --
# Returns a proper name for a color, given its intensities.
#
# Arguments:
# r, g, b -	Intensities on a 0-255 scale.

proc cname {r g b} {
    format #%02x%02x%02x $r $g $b
}
proc cname4 {r g b} {
    format #%04x%04x%04x $r $g $b
}

# mkColors --
# Creates a canvas and fills it with a 2-D array of squares, each of a
# different color.
#
# Arguments:
# c -		Name of canvas window to create.
# width -	Number of squares in each row.
# height -	Number of squares in each column.
# r, g, b -	Initial value for red, green, and blue intensities.
# rx, gx, bx -	Change in intensities between adjacent elements in row.
# ry, gy, by -	Change in intensities between adjacent elements in column.

proc mkColors {c width height r g b rx gx bx ry gy by} {
    catch {destroy $c}
    canvas $c -width 400 -height 200 -bd 0
    for {set y 0} {$y < $height} {incr y} {
	for {set x 0} {$x < $width} {incr x} {
	    set color [format #%02x%02x%02x [expr $r + $y*$ry + $x*$rx] \
		    [expr $g + $y*$gy + $x*$gx] [expr $b + $y*$by + $x*$bx]]
	    $c create rectangle [expr 10*$x] [expr 20*$y] \
		    [expr 10*$x + 10] [expr 20*$y + 20] -outline {} \
		    -fill $color
	}
    }
}

# closest -
# Given intensities between 0 and 255, return the closest intensities
# that the server can provide.
#
# Arguments:
# w -		Window in which to lookup color
# r, g, b -	Desired intensities, between 0 and 255.

proc closest {w r g b} {
    set vals [winfo rgb $w [cname $r $g $b]]
    list [expr [lindex $vals 0]/256] [expr [lindex $vals 1]/256] \
	    [expr [lindex $vals 2]/256]
}

# c255  -
# Given a list of red, green, and blue intensities, scale them
# down to a 0-255 range.
#
# Arguments:
# vals -	List of intensities.

proc c255 {vals} {
    list [expr [lindex $vals 0]/256] [expr [lindex $vals 1]/256] \
	    [expr [lindex $vals 2]/256]
}

# colorsFree --
#
# Returns 1 if there appear to be free colormap entries in a window,
# 0 otherwise.
#
# Arguments:
# w -			Name of window in which to check.
# red, green, blue -	Intensities to use in a trial color allocation
#			to see if there are colormap entries free.

proc colorsFree {w {red 31} {green 245} {blue 192}} {
    set vals [winfo rgb $w [format #%02x%02x%02x $red $green $blue]]
    expr ([lindex $vals 0]/256 == $red) && ([lindex $vals 1]/256 == $green) \
	    && ([lindex $vals 2]/256 == $blue)
}

# Create a top-level with its own colormap (so we can test under
# controlled conditions), then check to make sure that the visual
# is color-mapped with 256 colors.  If not, just skip this whole
# test file.

if [catch {toplevel .t -visual {pseudocolor 8} -colormap new}] {
    ::tcltest::cleanupTests
    return
}
wm geom .t +0+0
if {[winfo depth .t] != 8} {
    destroy .t
    ::tcltest::cleanupTests
    return
}
mkColors .t.c 40 6 0 0 0 0 6 0 0 0 40
pack .t.c
update
if ![colorsFree .t.c 101 233 17] {
    destroy .t
    ::tcltest::cleanupTests
    return
}
mkColors .t.c2 20 1 250 0 0 -10 0 0 0 0 0
pack .t.c2
if [colorsFree .t.c] {
    destroy .t
    ::tcltest::cleanupTests
    return
}
destroy .t.c .t.c2

test color-1.1 {Tk_AllocColorFromObj - converting internal reps} {
    set x green
    lindex $x 0
    destroy .b1
    button .b1 -foreground $x -text .b1
    lindex $x 0
    testcolor green
} {{1 0}}
test color-1.2 {Tk_AllocColorFromObj - discard stale color} {
    set x green
    destroy .b1 .b2
    button .b1 -foreground $x -text First
    destroy .b1
    set result {}
    lappend result [testcolor green]
    button .b2 -foreground $x -text Second
    lappend result [testcolor green]
} {{} {{1 1}}}
test color-1.3 {Tk_AllocColorFromObj - reuse existing color} {
    set x green
    destroy .b1 .b2
    button .b1 -foreground $x -text First
    set result {}
    lappend result [testcolor green]
    button .b2 -foreground $x -text Second
    pack .b1 .b2 -side top
    lappend result [testcolor green]
} {{{1 1}} {{2 1}}}
test color-1.4 {Tk_AllocColorFromObj - try other colors in list} {
    set x purple
    destroy .b1 .b2 .t.b
    button .b1 -foreground $x -text First
    pack .b1 -side top
    set result {}
    lappend result [testcolor purple]
    button .t.b -foreground $x -text Second
    pack .t.b -side top
    lappend result [testcolor purple]
    button .b2 -foreground $x -text Third
    pack .b2 -side top
    lappend result [testcolor purple]
} {{{1 1}} {{1 1} {1 0}} {{1 0} {2 1}}}

test color-2.1 {Tk_GetColor procedure} {
    c255 [winfo rgb .t #FF0000]
} {255 0 0}
test color-2.2 {Tk_GetColor procedure} {
    list [catch {winfo rgb .t noname} msg] $msg
} {1 {unknown color name "noname"}}
test color-2.3 {Tk_GetColor procedure} {
    c255 [winfo rgb .t #123456]
} {18 52 86}
test color-2.4 {Tk_GetColor procedure} {
    list [catch {winfo rgb .t #xyz} msg] $msg
} {1 {invalid color name "#xyz"}}
test color-2.5 {Tk_GetColor procedure} {
    winfo rgb .t #00FF00
} {0 65535 0}
test color-2.6 {Tk_GetColor procedure} {nonPortable} {
    # Red doesn't always map to *pure* red
    winfo rgb .t red
} {65535 0 0}
test color-2.7 {Tk_GetColor procedure} {
    winfo rgb .t #ff0000
} {65535 0 0}

test color-3.1 {Tk_FreeColor procedure, reference counting} {
    eval destroy [winfo child .t]
    mkColors .t.c 40 6 0 240 240 0 -6 0 0 0 -40
    pack .t.c
    mkColors .t.c2 20 1 250 0 0 -10 0 0 0 0 0
    pack .t.c2
    update
    set last [.t.c2 create rectangle 50 50 70 60 -outline {} \
	    -fill [cname 0 240 240]]
    .t.c delete 1
    set result [colorsFree .t]
    .t.c2 delete $last
    lappend result [colorsFree .t]
} {0 1}
test color-3.2 {Tk_FreeColor procedure, flushing stressed cmap information} {
    eval destroy [winfo child .t]
    mkColors .t.c 40 6 0 240 240 0 -6 0 0 0 -40
    pack .t.c
    mkColors .t.c2 20 1 250 0 0 -10 0 0 0 0 0
    mkColors .t.c2 20 1 250 250 0 -10 -10 0 0 0 0
    pack .t.c2
    update
    closest .t 241 241 1
} {240 240 0}
test color-3.3 {Tk_FreeColorFromObj - reference counts} {
    set x purple
    destroy .b1 .b2 .t.b
    button .b1 -foreground $x -text First
    pack .b1 -side top
    button .t.b -foreground $x -text Second
    pack .t.b -side top
    button .b2 -foreground $x -text Third
    pack .b2 -side top
    set result {}
    lappend result [testcolor purple]
    destroy .b1
    lappend result [testcolor purple]
    destroy .b2
    lappend result [testcolor purple]
    destroy .t.b
    lappend result [testcolor purple]
} {{{1 0} {2 1}} {{1 0} {1 1}} {{1 0}} {}}
test color-3.4 {Tk_FreeColorFromObj - unlinking from list} {
    destroy .b .t.b .t2 .t3
    toplevel .t2 -visual {pseudocolor 8} -colormap new
    toplevel .t3 -visual {pseudocolor 8} -colormap new
    set x purple
    button .b -foreground $x -text .b1
    button .t.b1 -foreground $x -text .t.b1
    button .t.b2 -foreground $x -text .t.b2
    button .t2.b1 -foreground $x -text .t2.b1
    button .t2.b2 -foreground $x -text .t2.b2
    button .t2.b3 -foreground $x -text .t2.b3
    button .t3.b1 -foreground $x -text .t3.b1
    button .t3.b2 -foreground $x -text .t3.b2
    button .t3.b3 -foreground $x -text .t3.b3
    button .t3.b4 -foreground $x -text .t3.b4
    set result {}
    lappend result [testcolor purple]
    destroy .t2
    lappend result [testcolor purple]
    destroy .b
    lappend result [testcolor purple]
    destroy .t3
    lappend result [testcolor purple]
    destroy .t
    lappend result [testcolor purple]
} {{{4 1} {3 0} {2 0} {1 0}} {{4 1} {2 0} {1 0}} {{4 1} {2 0}} {{2 0}} {}}

test color-4.1 {FreeColorObjProc} {
    destroy .b
    set x [format purple]
    button .b -foreground $x -text .b1
    set y [format purple]
    .b configure -foreground $y
    set z [format purple]
    .b configure -foreground $z
    set result {}
    lappend result [testcolor purple]
    set x red
    lappend result [testcolor purple]
    set z 32
    lappend result [testcolor purple]
    destroy .b
    lappend result [testcolor purple]
    set y bogus
    set result
} {{{1 3}} {{1 2}} {{1 1}} {}}

destroy .t

# cleanup
::tcltest::cleanupTests
return