globals
[ old-style
  old-depth
  old-tint
  old-hue
  old-wave-width
]

to startup setup end

;; to enable selection of a random style,
;; report the list of pre-defined style names in chooser control
;; (to maintain: after updating chooser control, copy contents of control
;; then paste to replace list in square brackets) 
to-report styles report [
   "flat"
"checked"
"dappled"
"gradient-horizontal"
"gradient-h-dithered"
"gradient-vertical"
"gradient-v-dithered"
"gradient-diagonal"
"gradient-d-dithered"
]
end

to setup
   ca
   set style one-of styles
end

to execute
   setup-background style
end

;; applies a background style selected by number or by name
;; "styles" reporter reports the list of legal style names
to setup-background [ #style ]
   let base-color hue + tint
   ;; create command to execute style on patches
   let style-command (word "ask patches [ set pcolor " #style " base-color depth ]" )
   ;; execute the command
   run style-command
   
   ;; note that if you just wanted to apply a particular style
   ;; (rather than allowing for selection among different styles)
   ;; you would just run the style command directly:
   ;; e.g.: ask patches [ set pcolor checked base-color depth ] 
end 


;; helper reporters to scale pxcor and pycor to values between 0.0 and 1.0

to-report gx report ( ( pxcor - min-pxcor ) / world-width ) end
to-report gy report ( ( pycor - min-pycor ) / world-height ) end
to-report randomness report ( .1 * ( 1 - random 3 )) end  

;; report the pcolor for "this" patch using a particular style
to-report flat [ #hue #diff ] report #hue end
to-report checked [ #hue #diff ] report #hue + #diff * ( ( pxcor + pycor ) mod 2 ) end
to-report dappled [ #hue #diff ] report #hue + random-float #diff end   
to-report gradient-horizontal [ #hue #diff ] report #hue + #diff * gx  end    
to-report gradient-vertical [ #hue #diff ] report #hue + #diff * gy end
to-report gradient-diagonal [ #hue #diff ] report #hue + .5 * #diff * (gx + gy) end
to-report gradient-h-dithered [ #hue #diff ] report #hue + #diff * gx + randomness end
to-report gradient-v-dithered [ #hue #diff ] report #hue + #diff * gy + randomness end  
to-report gradient-d-dithered [ #hue #diff ] report #hue + #diff * .5 * (gx + gy) + randomness end  

;; used to obtain appropriate minimum and maximum values for the tint slider
to-report min-tint report precision -4.9 1 end
to-report max-tint report precision (  4.9 - depth ) 1 end
  
to randomize
   ;; randomizes the display settings
   let tint-span max-tint - min-tint
   
   set hue 5 + 10 * random 13
   set depth precision (.5 + .1 * random 42) 1
   set tint precision (min-tint + precision ( random-float tint-span ) 1) 1
   set style one-of styles
   end

to-report control-monitor
   ;; monitors the values of the controls
   ;; updates the display using the new settings
   
   every 6 [ if auto-change? [ randomize ] ]
   if-else   depth != old-depth
          or style != old-style
          or tint != old-tint
          or hue != old-hue
   [ set old-depth depth
     set old-style style
     set old-tint tint
     set old-hue hue
     execute
     tick
     report "changed" 
   ]
   [ report (word "monitoring " (item ((floor timer) mod 3 ) [ "." ".." "..." ]) )
   ]
end
      
to-report hue-name report item floor ((hue - 5) / 10)
   [ "red" "orange" "yellow" "brown" "" "" "" "" "" "" "" "" "" "" "" ]
end   
      
@#$#@#$#@
GRAPHICS-WINDOW
150
75
462
408
75
75
2.0
1
10
1
1
1
0
0
0
1
-75
75
-75
75
1
1
0
ticks

SLIDER
45
130
140
163
depth
depth
.1
9.8
1.9
.1
1
NIL
HORIZONTAL

SLIDER
45
165
140
198
hue
hue
5
135
25
10
1
NIL
HORIZONTAL

SLIDER
45
200
140
233
tint
tint
min-tint
max-tint
-0.6
.1
1
NIL
HORIZONTAL

BUTTON
45
240
140
273
NIL
randomize
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL

CHOOSER
10
76
140
121
style
style
"flat" "checked" "dappled" "gradient-horizontal" "gradient-h-dithered" "gradient-vertical" "gradient-v-dithered" "gradient-diagonal" "gradient-d-dithered"
1

MONITOR
10
320
140
365
NIL
control-monitor
3
1
11

TEXTBOX
15
10
435
66
Select a style from the style chooser. Adjust other parameters as desired. The selected background pattern will be displayed in the view.\n\nIf \"auto-change?\" is \"On\", the style will change automatically every 10 seconds.
11
0.0
0

SWITCH
10
280
140
313
auto-change?
auto-change?
1
1
-1000

@#$#@#$#@
== WHAT IS IT? ==

This model demonstrates techniques to create attractive backgrounds for models.

In models where patch colors are not used as part of the information visualization, and where "prettyfication" is not inappropriate, patch colors can be assigned colors to form a pleasing background.

In models such as in educational modules, attractive and varied background patterns may halp capture and maintain user attention.

== HOW IT WORKS ==

By applying a particular algorithm for each patch, the set of patches can be made to adopt colors that create an eye pleasing pattern. This pattern forms as attractive background for other display elements of the model.

== HOW TO USE IT ==

Select a background pattern style from the chooser. Vary the parameters of the style by changing the sliders. A sample of the selected style is displayed dynamically in the view.

Click the "randomize" button to have NetLogo set a random background style and parameters.

== TRICKS AND TIPS ==

=== Using Reporters with "Special Effects" ===

This model works without a SETUP, RESET, or GO button. This model takes advantage of the fact that monitors execute periodically, even when there is no button activity. This works by using a monitor to run a reporter that periodically detect changes in the controls and update the display when needed. 

A reporter that does other things besides report a value, things that may affect the model's display or behavior, is said to have "special effects". In general it poor programming practice to create models with special effects. But, in some cases, such as this one, special effects can be put to good use.

=== Using RUN to Execute a Procedure from a Slider ===

=== Using helper reporters to Visually Simplify Complex Code ===

The use of GX, GY, and RANDOMNESS helps avoid error in the style reporters, and also makes those reporters concise and much easier for the human to interpret.

<info>
<summary>A playground for creating pleasing textured backgrounds</summary>
<copy>Copyright © 2007,2009 James P. Steiner</copy>
</info>
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250

hub
false
10
Circle -13345367 true true 0 0 300
Polygon -1 true false 75 75 165 30 225 30 150 75
Polygon -16777216 true false 165 90 240 45 240 195 165 255
Rectangle -16777216 false false 75 90 150 255

node
false
5
Circle -10899396 true true 0 0 300
Rectangle -16777216 false false 90 60 225 165
Polygon -16777216 false false 90 180 45 225 195 225 225 180
Rectangle -16777216 false false 105 75 210 150

packet
false
2
Circle -955883 true true 60 60 180

@#$#@#$#@
NetLogo 4.1beta1
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
default
0.0
-0.2 0 0.0 1.0
0.0 1 1.0 0.0
0.2 0 0.0 1.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180

@#$#@#$#@
0
@#$#@#$#@
