Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler scaling #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions src/Style/Scale.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ Then, when setting font sizes you can use:

Font.size (scaled 1) -- results in 16

Font.size (scaled 2) -- 16 * (1.618 ^ 2) results in 25.8
Font.size (scaled 2) -- 16 * 1.618 results in 25.8

Font.size (scaled 4) -- 16 * 1.618 ^ (4 - 1) results in 67.8

We can also provide negative numbers to scale below 16px.

Font.size (scaled -1) -- 16 * 1.618 ^ (-1) results in 9.9

@docs modular, roundedModular

-}


{-| -}
{-| Given a normal size and a ratio, returns a function which takes a scale parameter and returns a new size
-}
modular : Float -> Float -> Int -> Float
modular normal ratio fontScale =
resize normal ratio fontScale
Expand All @@ -38,25 +43,9 @@ roundedModular normal ratio =

resize : Float -> Float -> Int -> Float
resize normal ratio scale =
if scale == 0 || scale == 1 then
if scale == 0 then
normal
else if scale < 0 then
shrink ratio (scale * -1) normal
else
grow ratio (scale - 1) normal


grow : Float -> Int -> Float -> Float
grow ratio i size =
if i <= 0 then
size
else
grow ratio (i - 1) (size * ratio)


shrink : Float -> Int -> Float -> Float
shrink ratio i size =
if i <= 0 then
size
normal * ratio ^ (toFloat scale)
else
shrink ratio (i - 1) (size / ratio)
normal * ratio ^ (toFloat scale - 1)
47 changes: 47 additions & 0 deletions tests/ScaleTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module ScaleTest exposing (..)

import Expect
import Style.Scale exposing (roundedModular)
import Fuzz exposing (intRange, floatRange)
import Test exposing (..)


fakeModular : Float -> Float -> Int -> Float
fakeModular a b n =
if n == 0 then
a
else if n < 0 then
a * b ^ (toFloat n)
else
a * b ^ (toFloat n - 1)


fakeRounded a b =
toFloat << round << fakeModular a b


expecter : Float -> Float -> Int -> Expect.Expectation
expecter a b n =
(fakeRounded a b n == roundedModular a b n)
|> Expect.true "implementations don't match"


normal =
floatRange 1 200


ratio =
floatRange 1 3


exponent =
intRange -10 10


suite =
fuzz3
normal
ratio
exponent
"Check that the implementation matches my implementation"
expecter