Skip to content

Commit 4a0ebc7

Browse files
committed
Add color input, closes #5
1 parent f74bdc2 commit 4a0ebc7

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"purescript-signal": "^5.0.0",
2121
"purescript-foldable-traversable": "^0.4.1",
2222
"purescript-canvas": "^0.4.0",
23+
"purescript-colors": "^0.1.4",
2324
"purescript-drawing": "^0.4.0",
2425
"purescript-smolder": "^3.0.1"
2526
}

src/Flare.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,17 @@ exports.toFieldset = function(label) {
314314
};
315315
};
316316

317+
exports.cColor = createComponent("color",
318+
function(initial) {
319+
var input = document.createElement("input");
320+
input.type = "color";
321+
input.value = initial;
322+
return input;
323+
},
324+
"input",
325+
function(t, initial) {
326+
return t.value;
327+
}
328+
);
329+
317330
// vim: ts=2:sw=2

src/Flare.purs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ module Flare
2929
, select_
3030
, radioGroup
3131
, radioGroup_
32+
, color
33+
, color_
3234
, fieldset
3335
, applyUIFlipped
3436
, (<**>)
@@ -45,14 +47,16 @@ module Flare
4547
import Prelude
4648

4749
import Data.Array (head, catMaybes)
48-
import Data.Maybe (Maybe(..))
50+
import Data.Maybe (Maybe(..), fromMaybe)
4951
import Data.Monoid (class Monoid, mempty)
5052
import Data.Foldable (traverse_)
5153
import Data.Traversable (traverse)
5254

5355
import Control.Apply (lift2)
5456
import Control.Monad.Eff (Eff)
5557

58+
import Color (Color, toHexString, fromHexString)
59+
5660
import DOM (DOM)
5761
import DOM.Node.Types (Element())
5862

@@ -148,6 +152,7 @@ foreign import cBoolean :: CreateComponent Boolean
148152
foreign import cButton :: forall a. a -> CreateComponent a
149153
foreign import cSelect :: forall a. Array a -> (a -> String) -> CreateComponent a
150154
foreign import cRadioGroup :: forall a. Array a -> (a -> String) -> CreateComponent a
155+
foreign import cColor :: CreateComponent String
151156

152157
-- | Set up the HTML element for a given component and create the corresponding
153158
-- | signal channel.
@@ -285,6 +290,15 @@ radioGroup label default xs toString = createUI (cRadioGroup xs toString) label
285290
radioGroup_ :: forall e a. a -> Array a -> (a -> String) -> UI e a
286291
radioGroup_ = radioGroup ""
287292

293+
-- | Creates a color picker input field from a label and default `Color`.
294+
color :: forall e. Label -> Color -> UI e Color
295+
color label default = (fromMaybe default <<< fromHexString) <$>
296+
createUI cColor label (toHexString default)
297+
298+
-- | Like `color`, but without a label.
299+
color_ :: forall e. Color -> UI e Color
300+
color_ = color ""
301+
288302
foreign import toFieldset :: Label -> Array Element -> Element
289303

290304
-- | Group the components of a UI inside a fieldset element with a given title.

test/Main.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ ui6 = (greet <$> (select "Language" English [French, German] toString))
6262

6363
-- Example 7
6464

65-
plot m n1 s time =
66-
filled (fillColor (hsl 333.0 0.6 0.5)) $
65+
plot m n1 s col time =
66+
filled (fillColor col) $
6767
path (map point angles)
6868

6969
where point phi = { x: 100.0 + radius phi * cos phi
@@ -81,6 +81,7 @@ plot m n1 s time =
8181
ui7 = plot <$> (numberSlider "m" 0.0 10.0 1.0 7.0)
8282
<*> (numberSlider "n1" 1.0 10.0 0.1 4.0)
8383
<*> (numberSlider "s" 4.0 16.0 0.1 14.0)
84+
<*> (color "Color" (hsl 333.0 0.6 0.5))
8485
<*> lift animationFrame
8586

8687
-- Example 8

0 commit comments

Comments
 (0)