Skip to content

Commit cace99a

Browse files
committed
Documentation cleanup.
Also, deprecate the hue, saturation, luminosity, and alpha functions. These properties can be extracted by pattern matching, and there's no pedagogical need to use them earlier in the learning process.
1 parent 9fd3161 commit cace99a

File tree

6 files changed

+294
-133
lines changed

6 files changed

+294
-133
lines changed

codeworld-base/src/Internal/CodeWorld.hs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,83 @@ instance Show LiteralException where
5454
traced :: (a, CWT.Text) -> a
5555
traced (x, msg) = CW.trace (CWT.fromCWText msg) x
5656

57+
-- | A computer program.
58+
--
59+
-- This is a specific task or application that a computer performs. In
60+
-- CodeWorld, programs are defined using one of the "entry point"
61+
-- functions, such as 'drawingOf', 'animationOf', or 'activityOf'.
5762
type Program = IO ()
5863

64+
-- | A program that displays a drawing of a single picture.
65+
--
66+
-- This is the simplest way to define a program in CodeWorld. The
67+
-- argument is a 'Picture'.
68+
--
69+
-- For example:
70+
--
71+
-- > program = drawingOf(codeWorldLogo)
5972
drawingOf :: Picture -> Program
6073
drawingOf pic = CW.drawingOf (toCWPic pic) `catch` reportError
6174

75+
-- | A program that shows an animation changing over time.
76+
--
77+
-- The argument is a function, which maps each time (a 'Number' in
78+
-- seconds since the program started) to the 'Picture' that is shown
79+
-- at that time.
80+
--
81+
-- For example:
82+
--
83+
-- > program = animationOf(f)
84+
-- > f(t) = rotated(rectangle(5,5), 45 * t)
6285
animationOf :: (Number -> Picture) -> Program
6386
animationOf f = CW.animationOf (toCWPic . f . fromDouble) `catch` reportError
6487

88+
-- | A program that can interact with a user by responding to pointer
89+
-- and keyboard events and remember state.
90+
--
91+
-- To create an activity, you first choose a type for its state,
92+
-- called the "world" type. You will then describe the activity
93+
-- with three arguments:
94+
--
95+
-- 1. A function to create an initial world. The argument to
96+
-- this function is an infinite sequence of random numbers,
97+
-- which you can use to create a randomly chosen world.
98+
-- 2. A function that describes how the world changes when
99+
-- things happen. The function receives an old world and
100+
-- an 'Event' that occurs, and maps it to a new world.
101+
-- 3. A function that converts a world into a 'Picture', used
102+
-- to display the program on the screen.
103+
--
104+
-- For example:
105+
--
106+
-- > program = activityOf(initial, change, picture)
107+
-- >
108+
-- > initial(rs) = 0
109+
-- >
110+
-- > change(x, KeyPress("A")) = x - 1
111+
-- > change(x, KeyPress("D")) = x + 1
112+
-- > change(x, other) = x
113+
-- >
114+
-- > picture(x) = translated(solidCircle(1), x, 0)
115+
--
116+
-- In mathematical language, an activity is called a dynamical
117+
-- system. The world type is known as the phase space of the
118+
-- system, and the @change@ function is called the dynamics of
119+
-- the system. The @initial@ and @picture@ functions are not
120+
-- part of the system, but describe how the program should
121+
-- simulate and display it.
65122
activityOf ::
66123
( [Number] -> world
67124
, (world, Event) -> world
68125
, world -> Picture)
69126
-> Program
70127
activityOf (initial, event, draw) = interactionOf (initial, fst, event, draw)
71128

129+
-- | A program that acts just like one built with 'activityOf',
130+
-- but offers more tools to pause, rewind, and otherwise
131+
-- understand the program behavior. This can be used during
132+
-- development, and then changed back to 'activityOf' once the
133+
-- program is complete.
72134
debugActivityOf ::
73135
( [Number] -> world
74136
, (world, Event) -> world
@@ -77,6 +139,23 @@ debugActivityOf ::
77139
debugActivityOf (initial, event, draw) =
78140
debugInteractionOf (initial, fst, event, draw)
79141

142+
-- | A program that interacts with multiple different users by
143+
-- responding to their pointer and keyboard events.
144+
--
145+
-- The arguments to this function are similar 'activityOf',
146+
-- except that:
147+
--
148+
-- 1. A first argument, a 'Number' gives the desired number of
149+
-- players.
150+
-- 2. The @change@ function receives an extra argument telling
151+
-- which player intiated the event.
152+
-- 3. The @picture@ function receives an extra argument with
153+
-- the player for whom the picture should be built.
154+
--
155+
-- The activity will always begin with a "lobby", where players
156+
-- can create new games or join existing games with a code.
157+
-- Once the desired number of players have joined, the activity
158+
-- will begin.
80159
groupActivityOf ::
81160
( Number
82161
, [Number] -> state

codeworld-base/src/Internal/Color.hs

Lines changed: 126 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,25 @@ import Internal.Truth
2727
import qualified "base" Prelude as P
2828
import "base" Prelude ((.))
2929

30+
-- | A color.
31+
--
32+
-- Colors can be described in several ways:
33+
--
34+
-- 1. Using common color names: 'red', 'blue', 'yellow', 'green',
35+
-- 'orange', 'purple', 'pink', 'black', 'white', 'brown', and
36+
-- 'gray'.
37+
-- 2. Transforming other colors with functions such as 'light',
38+
-- 'dark', 'bright', 'dull', 'translucent', and 'mixed'.
39+
-- 3. Constructing colors from coordinates in a color space, such
40+
-- as 'RGB', 'RGBA', or 'HSL'.
41+
--
42+
-- Note that transparency is included in a color. Common color
43+
-- names and the 'RGB' and 'HSL' constructors only produce opaque
44+
-- colors, but 'RGBA' and the 'translucent' function work with
45+
-- transparency.
3046
newtype Color = Color { toCWColor :: CW.Color } deriving (P.Eq)
47+
48+
-- | A synonym for 'Color', using the non-US spelling.
3149
type Colour = Color
3250

3351
{-# RULES
@@ -72,36 +90,95 @@ fromHSL :: (Number, Number, Number) -> Color
7290
fromHSL (h, s, l) =
7391
Color (CW.HSL (toDouble (pi * h / 180)) (toDouble s) (toDouble l))
7492

93+
-- | Produces a color by mixing other colors in equal proportion.
94+
--
95+
-- The order of colors is unimportant. Colors may be mixed in uneven
96+
-- proportions by listing a color more than once, such as
97+
-- @mixed([red, red, orange])@.
7598
mixed :: [Color] -> Color
7699
mixed = Color . CW.mixed . P.map toCWColor
77100

101+
-- | Increases the luminosity of a color by the given amount.
102+
--
103+
-- The amount should be between -1 and 1, where:
104+
--
105+
-- * @lighter(c, 1)@ is always white, regardless of @c@.
106+
-- * @lighter(c, 0)@ is the same as @c@.
107+
-- * @lighter(c, -1)@ is always black, regardless of @c@.
78108
lighter :: (Color, Number) -> Color
79109
lighter (c, d) = Color (CW.lighter (toDouble d) (toCWColor c))
80110

111+
-- | Produces a lighter shade of the given color.
112+
--
113+
-- This function may be nested more than once to produce an even
114+
-- lighter shade, as in @light(light(blue))@.
81115
light :: Color -> Color
82116
light = Color . CW.light . toCWColor
83117

118+
-- | Decreases the luminosity of a color by the given amount.
119+
--
120+
-- The amount should be between -1 and 1, where:
121+
--
122+
-- * @darker(c, 1)@ is always black, regardless of @c@.
123+
-- * @darker(c, 0)@ is the same as @c@.
124+
-- * @darker(c, -1)@ is always white, regardless of @c@.
84125
darker :: (Color, Number) -> Color
85126
darker (c, d) = Color (CW.darker (toDouble d) (toCWColor c))
86127

128+
-- | Produces a darker shade of the given color.
129+
--
130+
-- This function may be nested more than once to produce an even
131+
-- darker shade, as in @dark(dark(green))@.
87132
dark :: Color -> Color
88133
dark = Color . CW.dark . toCWColor
89134

135+
-- | Increases the saturation of a color by the given amount.
136+
--
137+
-- The amount should be between -1 and 1, where:
138+
--
139+
-- * @brighter(c, 1)@ is a fully saturated version of @c@.
140+
-- * @brighter(c, 0)@ is the same as @c@.
141+
-- * @brighter(c, -1)@ is just a shade of gray with no color.
90142
brighter :: (Color, Number) -> Color
91143
brighter (c, d) = Color (CW.brighter (toDouble d) (toCWColor c))
92144

145+
-- | Produces a brighter shade of the given color; that is, less
146+
-- gray and more colorful.
147+
--
148+
-- This function may be nested more than once to produce an even
149+
-- brighter shade, as in @bright(bright(yellow))@.
93150
bright :: Color -> Color
94151
bright = Color . CW.bright . toCWColor
95152

153+
-- | Decreases the saturation of a color by the given amount.
154+
--
155+
-- The amount should be between -1 and 1, where:
156+
--
157+
-- * @duller(c, 1)@ is just a shade of gray with no color.
158+
-- * @duller(c, 0)@ is the same as @c@.
159+
-- * @duller(c, -1)@ is a fully saturated version of @c@.
96160
duller :: (Color, Number) -> Color
97161
duller (c, d) = Color (CW.duller (toDouble d) (toCWColor c))
98162

163+
-- | Produces a duller shade of the given color; that is, more
164+
-- gray and less colorful.
165+
--
166+
-- This function may be nested more than once to produce an even
167+
-- duller shade, as in @dull(dull(purple))@.
99168
dull :: Color -> Color
100169
dull = Color . CW.dull . toCWColor
101170

171+
-- | Produces a partially transparent color.
172+
--
173+
-- This function may be nested more than once to produce an even
174+
-- more transparent color, as in @translucent(translucent(brown))@.
102175
translucent :: Color -> Color
103176
translucent = Color . CW.translucent . toCWColor
104177

178+
-- | An infinite list of various colors.
179+
--
180+
-- The list is chosen to contain a variety of different hues as
181+
-- spread out as possible to create colorful effects.
105182
assortedColors :: [Color]
106183
assortedColors = P.map Color CW.assortedColors
107184

@@ -116,32 +193,65 @@ alpha = fromDouble . CW.alpha . toCWColor
116193

117194
-- New style colors
118195

119-
-- Old style colors
120-
121-
white, black, red, green, blue, cyan, magenta, yellow :: Color
122-
orange, rose, chartreuse, aquamarine, violet, azure :: Color
123-
gray, grey :: Color
124-
196+
-- | The color white
197+
white :: Color
125198
white = Color CW.white
199+
200+
-- | The color black
201+
black :: Color
126202
black = Color CW.black
203+
204+
-- | The color gray
205+
gray :: Color
127206
gray = Color CW.gray
207+
208+
-- | The color grey
209+
--
210+
-- This is the same color as 'gray', but with a non-US
211+
-- spelling.
212+
grey :: Color
128213
grey = Color CW.grey
214+
215+
-- | The color red
216+
red :: Color
129217
red = Color CW.red
218+
219+
-- | The color orange
220+
orange :: Color
130221
orange = Color CW.orange
222+
223+
-- | The color yellow
224+
yellow :: Color
131225
yellow = Color CW.yellow
226+
227+
-- | The color green
228+
green :: Color
132229
green = Color CW.green
230+
231+
-- | The color blue
232+
blue :: Color
133233
blue = Color CW.blue
234+
235+
-- | The color purple
236+
purple :: Color
134237
purple = Color CW.purple
238+
239+
-- | The color pink
240+
pink :: Color
135241
pink = Color CW.pink
242+
243+
-- | The color brown
244+
brown :: Color
136245
brown = Color CW.brown
137246

247+
cyan, magenta, rose, chartreuse, aquamarine, violet, azure :: Color
138248
cyan = Color CW.cyan
139249
magenta = Color CW.magenta
250+
rose = Color CW.rose
140251
chartreuse = Color CW.chartreuse
141252
aquamarine = Color CW.aquamarine
142-
azure = Color CW.azure
143253
violet = Color CW.violet
144-
rose = Color CW.rose
254+
azure = Color CW.azure
145255

146256
{-# WARNING magenta [ "Please use HSL(300, 0.75, 0.5) instead of magenta."
147257
, "The variable magenta may be removed July 2020." ] #-}
@@ -157,3 +267,11 @@ rose = Color CW.rose
157267
, "The variable rose may be removed July 2020." ] #-}
158268
{-# WARNING violet [ "Please use purple instead of violet."
159269
, "The variable violet may be removed July 2020." ] #-}
270+
{-# WARNING hue [ "Please match HSL(...) instead of using hue(...)."
271+
, "The hue function may be removed July 2020." ] #-}
272+
{-# WARNING saturation [ "Please match HSL(...) instead of using saturation(...)."
273+
, "The saturation function may be removed July 2020." ] #-}
274+
{-# WARNING luminosity [ "Please match HSL(...) instead of using luminosity(...)."
275+
, "The luminosity function may be removed July 2020." ] #-}
276+
{-# WARNING alpha [ "Please match RGBA(...) instead of using alpha(...)."
277+
, "The alpha function may be removed July 2020." ] #-}

0 commit comments

Comments
 (0)