on master help sign in
Merge pull request #245 from mgold/dev
66add54Evan Czaplicki
libraries/Color.elm+50−30
@@ -1,31 +1,52 @@
1
1
2module Color where
2module Color where
3
3
4+{-| Library for working with colors. Includes
5+[RGB](https://en.wikipedia.org/wiki/RGB_color_model) and
6+[HSV](http://en.wikipedia.org/wiki/HSL_and_HSV) creation, gradients, and
7+built-in names.
8+
9+# Creation
10+@docs rgb, rgba, hsv, hsva, grayscale, greyscale
11+
12+# From Other Colors
13+@docs complementary
14+
15+# Gradients
16+@docs linear radial
17+
18+# Built-in Colors
19+These come from the [Tango
20+palette](http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines).
21+@docs red, orange, yellow, green, blue, purple, brown, black, white, grey, gray, charcoal, lightRed, lightOrange, lightYellow, lightGreen, lightBlue, lightPurple, lightBrown, lightGrey, lightGray, lightCharcoal, darkRed, darkOrange, darkYellow, darkGreen, darkBlue, darkPurple, darkBrown, darkGrey, darkGray, darkCharcoal
22+
23+-}
24+
4import Native.Color
25import Native.Color
5import Basics ((-))
26import Basics ((-))
6
27
7data Color = Color Int Int Int Float
28data Color = Color Int Int Int Float
8
29
9−-- Create RGB colors with an alpha component for transparency.
30+{-| Create RGB colors with an alpha component for transparency.
10−-- The alpha component is specified with numbers between 0 and 1.
31+The alpha component is specified with numbers between 0 and 1. -}
11rgba : Int -> Int -> Int -> Float -> Color
32rgba : Int -> Int -> Int -> Float -> Color
12rgba = Color
33rgba = Color
13
34
14−-- Create RGB colors from numbers between 0 and 255 inclusive.
35+{-| Create RGB colors from numbers between 0 and 255 inclusive. -}
15rgb : Int -> Int -> Int -> Color
36rgb : Int -> Int -> Int -> Color
16rgb r g b = Color r g b 1
37rgb r g b = Color r g b 1
17
38
18−lightYellow = Color 255 233 79 1
39+lightRed = Color 239 41 41 1
19−yellow = Color 237 212 0 1
40+red = Color 204 0 0 1
20−darkYellow = Color 196 160 0 1
41+darkRed = Color 164 0 0 1
21
42
22lightOrange = Color 252 175 62 1
43lightOrange = Color 252 175 62 1
23orange = Color 245 121 0 1
44orange = Color 245 121 0 1
24darkOrange = Color 206 92 0 1
45darkOrange = Color 206 92 0 1
25
46
26−lightBrown = Color 233 185 110 1
47+lightYellow = Color 255 233 79 1
27−brown = Color 193 125 17 1
48+yellow = Color 237 212 0 1
28−darkBrown = Color 143 89 2 1
49+darkYellow = Color 196 160 0 1
29
50
30lightGreen = Color 138 226 52 1
51lightGreen = Color 138 226 52 1
31green = Color 115 210 22 1
52green = Color 115 210 22 1
@@ -39,9 +60,9 @@ lightPurple = Color 173 127 168 1
39purple = Color 117 80 123 1
60purple = Color 117 80 123 1
40darkPurple = Color 92 53 102 1
61darkPurple = Color 92 53 102 1
41
62
42−lightRed = Color 239 41 41 1
63+lightBrown = Color 233 185 110 1
43−red = Color 204 0 0 1
64+brown = Color 193 125 17 1
44−darkRed = Color 164 0 0 1
65+darkBrown = Color 143 89 2 1
45
66
46black = Color 0 0 0 1
67black = Color 0 0 0 1
47white = Color 255 255 255 1
68white = Color 255 255 255 1
@@ -58,25 +79,24 @@ lightCharcoal = Color 136 138 133 1
58charcoal = Color 85 87 83 1
79charcoal = Color 85 87 83 1
59darkCharcoal = Color 46 52 54 1
80darkCharcoal = Color 46 52 54 1
60
81
82+{-| Produce a gray based on the input. 0 is white, 1 is black. -}
61grayscale : Float -> Color
83grayscale : Float -> Color
62grayscale p = hsv 0 0 (1-p)
84grayscale p = hsv 0 0 (1-p)
63greyscale p = hsv 0 0 (1-p)
85greyscale p = hsv 0 0 (1-p)
64
86
65−-- Produce a “complementary color”.
87+{-| Produce a “complementary color”.
66−-- The two colors will accent each other.
88+The two colors will accent each other. -}
67complement : Color -> Color
89complement : Color -> Color
68complement = Native.Color.complement
90complement = Native.Color.complement
69
91
70−-- Create [HSV colors](http://en.wikipedia.org/wiki/HSL_and_HSV)
92+{-| Create [HSV colors](http://en.wikipedia.org/wiki/HSL_and_HSV)
71−-- with an alpha component for transparency.
93+with an alpha component for transparency. -}
72hsva : Float -> Float -> Float -> Float -> Color
94hsva : Float -> Float -> Float -> Float -> Color
73hsva = Native.Color.hsva
95hsva = Native.Color.hsva
74
96
75−-- Create [HSV colors](http://en.wikipedia.org/wiki/HSL_and_HSV).
97+{-| Create [HSV colors](http://en.wikipedia.org/wiki/HSL_and_HSV). This is very
76−-- This is very convenient for creating colors that cycle and shift.
98+convenient for creating colors that cycle and shift. Hue is an angle and should
77−-- Hue is an angle and should be given in standard Elm angles (radians).
99+be given in standard Elm angles (radians). -}
78−--
79−-- hsv (degrees 240) 1 1 == blue
80hsv : Float -> Float -> Float -> Color
100hsv : Float -> Float -> Float -> Color
81hsv = Native.Color.hsv
101hsv = Native.Color.hsv
82
102
@@ -84,17 +104,17 @@ data Gradient
84 = Linear (Float,Float) (Float,Float) [(Float,Color)]
104 = Linear (Float,Float) (Float,Float) [(Float,Color)]
85 | Radial (Float,Float) Float (Float,Float) Float [(Float,Color)]
105 | Radial (Float,Float) Float (Float,Float) Float [(Float,Color)]
86
106
87−-- Create a linear gradient. Takes a start and end point and then a series
107+{-| Create a linear gradient. Takes a start and end point and then a series of
88−-- of “color stops” that indicate how to interpolate between
108+“color stops” that indicate how to interpolate between the start and
89−-- the start and end points. See [this example](/edit/examples/Elements/LinearGradient.elm) for
109+end points. See [this example](/edit/examples/Elements/LinearGradient.elm) for a
90−-- a more visual explanation.
110+more visual explanation. -}
91linear : (number, number) -> (number, number) -> [(Float,Color)] -> Gradient
111linear : (number, number) -> (number, number) -> [(Float,Color)] -> Gradient
92linear = Linear
112linear = Linear
93
113
94−-- Create a radial gradient. First takes a start point and inner radius.
114+{-| Create a radial gradient. First takes a start point and inner radius. Then
95−-- Then takes an end point and outer radius. It then takes a series
115+takes an end point and outer radius. It then takes a series of “color
96−-- of “color stops” that indicate how to interpolate between
116+stops” that indicate how to interpolate between the inner and outer
97−-- the inner and outer circles. See [this example](/edit/examples/Elements/RadialGradient.elm) for
117+circles. See [this example](/edit/examples/Elements/RadialGradient.elm) for a
98−-- a more visual explanation.
118+more visual explanation. -}
99radial : (number,number) -> number -> (number,number) -> number -> [(Float,Color)] -> Gradient
119radial : (number,number) -> number -> (number,number) -> number -> [(Float,Color)] -> Gradient
100radial = Radial
120radial = Radial
libraries/Dict.elm+49−25
@@ -8,6 +8,30 @@ module Dict (empty,singleton,insert
8 ,toList,fromList
8 ,toList,fromList
9 ) where
9 ) where
10
10
11+{-|A dictionary mapping unique keys to values. The keys can be any comparable
12+type. This includes `Int`, `Float`, `Time`, `Char`, `String`, and tuples or
13+lists of comparable types.
14+
15+Insert, remove, and query operations all take *O(log n)* time.
16+
17+# Build
18+@docs empty, singleton, insert, remove
19+
20+# Query
21+@docs member, lookup, findWithDefault
22+
23+# Combine
24+@docs union, intersect, diff
25+
26+# Lists
27+@docs keys, values, toList, fromList
28+
29+# Transform
30+@docs map, foldl, foldr
31+
32+-}
33+
34+
11import open Basics
35import open Basics
12import open Maybe
36import open Maybe
13import Native.Error
37import Native.Error
@@ -18,7 +42,7 @@ data NColor = Red | Black
18
42
19data Dict k v = RBNode NColor k v (Dict k v) (Dict k v) | RBEmpty
43data Dict k v = RBNode NColor k v (Dict k v) (Dict k v) | RBEmpty
20
44
21−-- Create an empty dictionary.
45+{-| Create an empty dictionary. -}
22empty : Dict comparable v
46empty : Dict comparable v
23empty = RBEmpty
47empty = RBEmpty
24
48
@@ -116,7 +140,7 @@ max t =
116 }
140 }
117--}
141--}
118
142
119−-- Lookup the value associated with a key.
143+{-| Lookup the value associated with a key. -}
120lookup : comparable -> Dict comparable v -> Maybe v
144lookup : comparable -> Dict comparable v -> Maybe v
121lookup k t =
145lookup k t =
122 case t of
146 case t of
@@ -127,8 +151,8 @@ lookup k t =
127 EQ -> Just v
151 EQ -> Just v
128 GT -> lookup k r
152 GT -> lookup k r
129
153
130−-- Find the value associated with a key. If the key is not found,
154+{-| Find the value associated with a key. If the key is not found,
131−-- return the default value.
155+return the default value. -}
132findWithDefault : v -> comparable -> Dict comparable v -> v
156findWithDefault : v -> comparable -> Dict comparable v -> v
133findWithDefault base k t =
157findWithDefault base k t =
134 case t of
158 case t of
@@ -152,7 +176,7 @@ find k t =
152 }
176 }
153--}
177--}
154
178
155−-- Determine if a key is in a dictionary.
179+{-| Determine if a key is in a dictionary. -}
156member : comparable -> Dict comparable v -> Bool
180member : comparable -> Dict comparable v -> Bool
157-- Does t contain k?
181-- Does t contain k?
158member k t = isJust <| lookup k t
182member k t = isJust <| lookup k t
@@ -208,8 +232,8 @@ ensureBlackRoot t =
208 RBNode Red k v l r -> RBNode Black k v l r
232 RBNode Red k v l r -> RBNode Black k v l r
209 _ -> t
233 _ -> t
210
234
211−-- Insert a key-value pair into a dictionary. Replaces value when there is
235+{-| Insert a key-value pair into a dictionary. Replaces value when there is
212−-- a collision.
236+a collision. -}
213insert : comparable -> v -> Dict comparable v -> Dict comparable v
237insert : comparable -> v -> Dict comparable v -> Dict comparable v
214insert k v t = -- Invariant: t is a valid left-leaning rb tree
238insert k v t = -- Invariant: t is a valid left-leaning rb tree
215 let ins t =
239 let ins t =
@@ -231,7 +255,7 @@ insert k v t = -- Invariant: t is a valid left-leaning rb tree
231 else new_t)
255 else new_t)
232--}
256--}
233
257
234−-- Create a dictionary with one key-value pair.
258+{-| Create a dictionary with one key-value pair. -}
235singleton : comparable -> v -> Dict comparable v
259singleton : comparable -> v -> Dict comparable v
236singleton k v = insert k v RBEmpty
260singleton k v = insert k v RBEmpty
237
261
@@ -312,8 +336,8 @@ deleteMax t =
312 in ensureBlackRoot (del t)
336 in ensureBlackRoot (del t)
313--}
337--}
314
338
315−-- Remove a key-value pair from a dictionary. If the key is not found,
339+{-| Remove a key-value pair from a dictionary. If the key is not found,
316−-- no changes are made.
340+no changes are made. -}
317remove : comparable -> Dict comparable v -> Dict comparable v
341remove : comparable -> Dict comparable v -> Dict comparable v
318remove k t =
342remove k t =
319 let eq_and_noRightNode t =
343 let eq_and_noRightNode t =
@@ -346,58 +370,58 @@ remove k t =
346 Native.Error.raise "invariants broken after remove")
370 Native.Error.raise "invariants broken after remove")
347--}
371--}
348
372
349−-- Apply a function to all values in a dictionary.
373+{-| Apply a function to all values in a dictionary. -}
350map : (a -> b) -> Dict comparable a -> Dict comparable b
374map : (a -> b) -> Dict comparable a -> Dict comparable b
351map f t =
375map f t =
352 case t of
376 case t of
353 RBEmpty -> RBEmpty
377 RBEmpty -> RBEmpty
354 RBNode c k v l r -> RBNode c k (f v) (map f l) (map f r)
378 RBNode c k v l r -> RBNode c k (f v) (map f l) (map f r)
355
379
356−-- Fold over the key-value pairs in a dictionary, in order from lowest
380+{-| Fold over the key-value pairs in a dictionary, in order from lowest
357−-- key to highest key.
381+key to highest key. -}
358foldl : (comparable -> v -> b -> b) -> b -> Dict comparable v -> b
382foldl : (comparable -> v -> b -> b) -> b -> Dict comparable v -> b
359foldl f acc t =
383foldl f acc t =
360 case t of
384 case t of
361 RBEmpty -> acc
385 RBEmpty -> acc
362 RBNode _ k v l r -> foldl f (f k v (foldl f acc l)) r
386 RBNode _ k v l r -> foldl f (f k v (foldl f acc l)) r
363
387
364−-- Fold over the key-value pairs in a dictionary, in order from highest
388+{-| Fold over the key-value pairs in a dictionary, in order from highest
365−-- key to lowest key.
389+key to lowest key. -}
366foldr : (comparable -> v -> b -> b) -> b -> Dict comparable v -> b
390foldr : (comparable -> v -> b -> b) -> b -> Dict comparable v -> b
367foldr f acc t =
391foldr f acc t =
368 case t of
392 case t of
369 RBEmpty -> acc
393 RBEmpty -> acc
370 RBNode _ k v l r -> foldr f (f k v (foldr f acc r)) l
394 RBNode _ k v l r -> foldr f (f k v (foldr f acc r)) l
371
395
372−-- Combine two dictionaries. If there is a collision, preference is given
396+{-| Combine two dictionaries. If there is a collision, preference is given
373−-- to the first dictionary.
397+to the first dictionary. -}
374union : Dict comparable v -> Dict comparable v -> Dict comparable v
398union : Dict comparable v -> Dict comparable v -> Dict comparable v
375union t1 t2 = foldl insert t2 t1
399union t1 t2 = foldl insert t2 t1
376
400
377−-- Keep a key-value pair when its key appears in the second dictionary.
401+{-| Keep a key-value pair when its key appears in the second dictionary.
378−-- Preference is given to values in the first dictionary.
402+Preference is given to values in the first dictionary. -}
379intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
403intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
380intersect t1 t2 =
404intersect t1 t2 =
381 let combine k v t = if k `member` t2 then insert k v t else t
405 let combine k v t = if k `member` t2 then insert k v t else t
382 in foldl combine empty t1
406 in foldl combine empty t1
383
407
384−-- Keep a key-value pair when its key does not appear in the second dictionary.
408+{-| Keep a key-value pair when its key does not appear in the second dictionary.
385−-- Preference is given to the first dictionary.
409+Preference is given to the first dictionary. -}
386diff : Dict comparable v -> Dict comparable v -> Dict comparable v
410diff : Dict comparable v -> Dict comparable v -> Dict comparable v
387diff t1 t2 = foldl (\k v t -> remove k t) t1 t2
411diff t1 t2 = foldl (\k v t -> remove k t) t1 t2
388
412
389−-- Get all of the keys in a dictionary.
413+{-| Get all of the keys in a dictionary. -}
390keys : Dict comparable v -> [comparable]
414keys : Dict comparable v -> [comparable]
391keys t = foldr (\k v acc -> k :: acc) [] t
415keys t = foldr (\k v acc -> k :: acc) [] t
392
416
393−-- Get all of the values in a dictionary.
417+{-| Get all of the values in a dictionary. -}
394values : Dict comparable v -> [v]
418values : Dict comparable v -> [v]
395values t = foldr (\k v acc -> v :: acc) [] t
419values t = foldr (\k v acc -> v :: acc) [] t
396
420
397−-- Convert a dictionary into an association list of key-value pairs.
421+{-| Convert a dictionary into an association list of key-value pairs. -}
398toList : Dict comparable v -> [(comparable,v)]
422toList : Dict comparable v -> [(comparable,v)]
399toList t = foldr (\k v acc -> (k,v) :: acc) [] t
423toList t = foldr (\k v acc -> (k,v) :: acc) [] t
400
424
401−-- Convert an association list into a dictionary.
425+{-| Convert an association list into a dictionary. -}
402fromList : [(comparable,v)] -> Dict comparable v
426fromList : [(comparable,v)] -> Dict comparable v
403fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
427fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
libraries/Set.elm+38−14
@@ -6,61 +6,85 @@ module Set (empty,singleton,insert,remove
6 ,toList,fromList
6 ,toList,fromList
7 ) where
7 ) where
8
8
9+{-|A set of unique values. The values can be any comparable type. This includes
10+`Int`, `Float`, `Time`, `Char`, `String`, and tuples or lists of comparable
11+types.
12+
13+Insert, remove, and query operations all take *O(log n)* time.
14+
15+# Build
16+@docs empty, singleton, insert, remove
17+
18+# Query
19+@docs member
20+
21+# Combine
22+@docs union, intersect, diff
23+
24+# Lists
25+@docs toList, fromList
26+
27+# Transform
28+@docs map, foldl, foldr
29+
30+-}
31+
9import Maybe (Maybe)
32import Maybe (Maybe)
10import Dict as Dict
33import Dict as Dict
11import List as List
34import List as List
12
35
13type Set t = Dict.Dict t ()
36type Set t = Dict.Dict t ()
14
37
15−-- Create an empty set.
38+{-| Create an empty set. -}
16empty : Set comparable
39empty : Set comparable
17empty = Dict.empty
40empty = Dict.empty
18
41
19−-- Create a set with one value.
42+{-| Create a set with one value. -}
20singleton : comparable -> Set comparable
43singleton : comparable -> Set comparable
21singleton k = Dict.singleton k ()
44singleton k = Dict.singleton k ()
22
45
23−-- Insert a value into a set.
46+{-| Insert a value into a set. -}
24insert : comparable -> Set comparable -> Set comparable
47insert : comparable -> Set comparable -> Set comparable
25insert k = Dict.insert k ()
48insert k = Dict.insert k ()
26
49
27−-- Remove a value from a set. If the value is not found, no changes are made.
50+{-| Remove a value from a set. If the value is not found, no changes are made.
51+-}
28remove : comparable -> Set comparable -> Set comparable
52remove : comparable -> Set comparable -> Set comparable
29remove = Dict.remove
53remove = Dict.remove
30
54
31−-- Determine if a value is in a set.
55+{-| Determine if a value is in a set. -}
32member : comparable -> Set comparable -> Bool
56member : comparable -> Set comparable -> Bool
33member = Dict.member
57member = Dict.member
34
58
35−-- Get the union of two sets. Keep all values.
59+{-| Get the union of two sets. Keep all values. -}
36union : Set comparable -> Set comparable -> Set comparable
60union : Set comparable -> Set comparable -> Set comparable
37union = Dict.union
61union = Dict.union
38
62
39−-- Get the intersection of two sets. Keeps values that appear in both sets.
63+{-| Get the intersection of two sets. Keeps values that appear in both sets. -}
40intersect : Set comparable -> Set comparable -> Set comparable
64intersect : Set comparable -> Set comparable -> Set comparable
41intersect = Dict.intersect
65intersect = Dict.intersect
42
66
43−-- Get the difference between the first set and the second. Keeps values
67+{-| Get the difference between the first set and the second. Keeps values
44−-- that do not appear in the second set.
68+that do not appear in the second set. -}
45diff : Set comparable -> Set comparable -> Set comparable
69diff : Set comparable -> Set comparable -> Set comparable
46diff = Dict.diff
70diff = Dict.diff
47
71
48−-- Convert a set into a list.
72+{-| Convert a set into a list. -}
49toList : Set comparable -> [comparable]
73toList : Set comparable -> [comparable]
50toList = Dict.keys
74toList = Dict.keys
51
75
52−-- Convert a list into a set, removing any duplicates.
76+{-| Convert a list into a set, removing any duplicates. -}
53fromList : [comparable] -> Set comparable
77fromList : [comparable] -> Set comparable
54fromList xs = List.foldl insert empty xs
78fromList xs = List.foldl insert empty xs
55
79
56−-- Fold over the values in a set, in order from lowest to highest.
80+{-| Fold over the values in a set, in order from lowest to highest. -}
57foldl : (comparable -> b -> b) -> b -> Set comparable -> b
81foldl : (comparable -> b -> b) -> b -> Set comparable -> b
58foldl f b s = Dict.foldl (\k _ b -> f k b) b s
82foldl f b s = Dict.foldl (\k _ b -> f k b) b s
59
83
60−-- Fold over the values in a set, in order from highest to lowest.
84+{-| Fold over the values in a set, in order from highest to lowest. -}
61foldr : (comparable -> b -> b) -> b -> Set comparable -> b
85foldr : (comparable -> b -> b) -> b -> Set comparable -> b
62foldr f b s = Dict.foldr (\k _ b -> f k b) b s
86foldr f b s = Dict.foldr (\k _ b -> f k b) b s
63
87
64−-- Map a function onto a set, creating a new set with no duplicates.
88+{-| Map a function onto a set, creating a new set with no duplicates. -}
65map : (comparable -> comparable') -> Set comparable -> Set comparable'
89map : (comparable -> comparable') -> Set comparable -> Set comparable'
66map f s = fromList (List.map f (toList s))
90map f s = fromList (List.map f (toList s))