Skip to content

Instantly share code, notes, and snippets.

@r00k
Last active October 31, 2016 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r00k/70575039793934f0f4c5f268409a9618 to your computer and use it in GitHub Desktop.
Save r00k/70575039793934f0f4c5f268409a9618 to your computer and use it in GitHub Desktop.
-- Goal: add/substract a small random amount from each component (r, g, b, a)
-- of the passed-in color.
maybeMutateColor : Color -> Generator Color
maybeMutateColor color =
let
floatGenerator =
Random.float -maximumAlphaChange maximumAlphaChange
intGenerator =
Random.int -maximumRGBChange maximumRGBChange
colorGenerator =
intGenerator `Random.andThen` \dr ->
intGenerator `Random.andThen` \dg ->
intGenerator `Random.andThen` \db ->
floatGenerator `Random.andThen` \da ->
Random.Extra.constant
(Color.rgba
(color.r + dr)
(color.g + dg)
(color.b + db)
(color.a + da))
in
Random.Extra.frequency
[ ( 50.0, Random.Extra.constant color )
, ( 50.0, colorGenerator )
]
@r00k
Copy link
Author

r00k commented Oct 31, 2016

@xarvh - Can you help me uncrazy this?

@r00k
Copy link
Author

r00k commented Oct 31, 2016

Currently failing thusly:

-- TYPE MISMATCH -------------------------------------------------- src/Main.elm

The 1st and 2nd elements are different types of values.

135|             [ ( 50.0, Random.Extra.constant color )
136|>            , ( 50.0, colorGenerator )
137|             ]

The 1st element has this type:

    ( Float, Generator { a | a : Float, b : Int, g : Int, r : Int } )

But the 2nd is:

    ( Float, Generator Color )

@r00k
Copy link
Author

r00k commented Oct 31, 2016

Here's the current madness--I mean version:

adjustColor : Color -> Generator Int -> Generator Int -> Generator Int -> Generator Float -> Generator Color
adjustColor color dr dg db da =
    Color.rgba
        (dr `Random.andThen` \x -> Random.Extra.constant (color.r + x))
        (dg `Random.andThen` \x -> Random.Extra.constant (color.g + x))
        (db `Random.andThen` \x -> Random.Extra.constant (color.b + x))
        (da `Random.andThen` \x -> Random.Extra.constant (color.a + x))


maybeMutateColor : Color -> Generator Color
maybeMutateColor color =
    let
        floatGenerator =
            Random.float -maximumAlphaChange maximumAlphaChange

        intGenerator =
            Random.int -maximumRGBChange maximumRGBChange

        colorGenerator =
            Random.map4
                (adjustColor color)
                intGenerator
                intGenerator
                intGenerator
                floatGenerator
    in
        Random.Extra.frequency
            [ ( 50.0, Random.Extra.constant color )
            , ( 50.0, colorGenerator )
            ]

@r00k
Copy link
Author

r00k commented Oct 31, 2016

adjustColor : Color -> Int -> Int -> Int -> Float -> Color
adjustColor color dr dg db da =
    Color.rgba
        (color.r + dr)
        (color.g + dg)
        (color.b + db)
        (color.a + da)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment