diff options
Diffstat (limited to 'src/_functions.scss')
| -rw-r--r-- | src/_functions.scss | 120 |
1 files changed, 45 insertions, 75 deletions
diff --git a/src/_functions.scss b/src/_functions.scss index 148a28c..aaa8ac9 100644 --- a/src/_functions.scss +++ b/src/_functions.scss | |||
| @@ -1,46 +1,47 @@ | |||
| 1 | @use 'sass:color'; | 1 | @use 'sass:color'; |
| 2 | @use 'sass:math'; | 2 | @use 'sass:math'; |
| 3 | @use 'sass:map'; | 3 | @use 'sass:map'; |
| 4 | @use 'sass:list'; | ||
| 4 | @use 'iro-sass/src/index' as iro; | 5 | @use 'iro-sass/src/index' as iro; |
| 5 | @use 'config'; | 6 | @use 'config'; |
| 6 | @use '@oddbird/blend'; | 7 | @use '@oddbird/blend'; |
| 7 | @use '@oddbird/blend/sass/convert' as blend-convert; | 8 | @use '@oddbird/blend/sass/convert' as blend-convert; |
| 8 | 9 | ||
| 9 | @function color($key, $tree: 'colors', $default: null, $global: false) { | 10 | @function color($key, $tree: iro.$props-default-tree, $default: null, $global: false) { |
| 10 | @return iro.props-get(join(--colors, $key), $tree, $default, $global); | 11 | @return iro.props-get(list.join(--colors, $key), $tree, $default, $global); |
| 11 | } | 12 | } |
| 12 | 13 | ||
| 13 | @function global-color($key, $tree: 'colors', $default: null, $global: true) { | 14 | @function global-color($key, $tree: iro.$props-default-tree, $default: null, $global: true) { |
| 14 | @return color($key, $tree, $default, $global); | 15 | @return color($key, $tree, $default, $global); |
| 15 | } | 16 | } |
| 16 | 17 | ||
| 17 | @function foreign-color($foreign-key, $key, $tree: 'colors', $default: null, $global: true) { | 18 | @function foreign-color($foreign-key, $key, $tree: iro.$props-default-tree, $default: null, $global: true) { |
| 18 | @return iro.props-get(join($foreign-key --colors, $key), $tree, $default, $global); | 19 | @return iro.props-get(list.join($foreign-key --colors, $key), $tree, $default, $global); |
| 19 | } | 20 | } |
| 20 | 21 | ||
| 21 | @function dim($key, $tree: 'dims', $default: null, $global: false) { | 22 | @function dim($key, $tree: iro.$props-default-tree, $default: null, $global: false) { |
| 22 | @return iro.props-get(join(--dims, $key), $tree, $default, $global); | 23 | @return iro.props-get(list.join(--dims, $key), $tree, $default, $global); |
| 23 | } | 24 | } |
| 24 | 25 | ||
| 25 | @function global-dim($key, $tree: 'dims', $default: null, $global: true) { | 26 | @function global-dim($key, $tree: iro.$props-default-tree, $default: null, $global: true) { |
| 26 | @return dim($key, $tree, $default, $global); | 27 | @return dim($key, $tree, $default, $global); |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 29 | @function foreign-dim($foreign-key, $key, $tree: 'dims', $default: null, $global: true) { | 30 | @function foreign-dim($foreign-key, $key, $tree: iro.$props-default-tree, $default: null, $global: true) { |
| 30 | @return iro.props-get(join($foreign-key --dims, $key), $tree, $default, $global); | 31 | @return iro.props-get(list.join($foreign-key --dims, $key), $tree, $default, $global); |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | @function font-prop($data, $overrides, $key, $prop) { | 34 | @function font-prop($data, $overrides, $key, $prop) { |
| 34 | @if (map-has-key($overrides, $prop)) { | 35 | @if (map.has-key($overrides, $prop)) { |
| 35 | @return map-get($overrides, $prop); | 36 | @return map.get($overrides, $prop); |
| 36 | } @else if (map-has-key($data, $prop)) { | 37 | } @else if (map.has-key($data, $prop)) { |
| 37 | @return global-dim(--font $key $prop); | 38 | @return global-dim(--font $key $prop); |
| 38 | } | 39 | } |
| 39 | @return null; | 40 | @return null; |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 42 | @function set-font($key, $overrides: ()) { | 43 | @function set-font($key, $overrides: ()) { |
| 43 | $font: iro.props-get-static(join(--dims --font, $key), 'dims', $global: true); | 44 | $font: iro.props-get-static(list.join(--dims --font, $key), $global: true); |
| 44 | 45 | ||
| 45 | $map: ( | 46 | $map: ( |
| 46 | font-family: font-prop($font, $overrides, $key, --family), | 47 | font-family: font-prop($font, $overrides, $key, --family), |
| @@ -57,47 +58,48 @@ | |||
| 57 | @return $map; | 58 | @return $map; |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 60 | @function multi-contrast($base-color, $colors, $wanted-contrasts) { | 61 | @function lum($color) { |
| 61 | $dir: if(lightness($base-color) >= 50%, -1, 1); | 62 | @return list.nth(blend-convert.lin_sRGB_to_XYZ(blend-convert.lin_sRGB(blend-convert.sassToRgb($color))), 2) + .05; |
| 62 | $base-lum: nth(blend-convert.lin_sRGB_to_XYZ(blend-convert.lin_sRGB(blend-convert.sassToRgb($base-color))), 2) + .05; | 63 | } |
| 64 | |||
| 65 | @function multi-contrast($base-color, $colors, $wanted-contrasts, $reference-color: $base-color) { | ||
| 66 | $ref-lum: lum($reference-color); | ||
| 63 | 67 | ||
| 64 | $result: (); | 68 | $result: (); |
| 65 | $colors-len: length($colors); | 69 | $colors-len: list.length($colors); |
| 66 | $colors-idx: if($dir == -1, $colors-len, 1); | 70 | $colors-idx: 1; |
| 67 | $wanted-len: length($wanted-contrasts); | 71 | $wanted-len: list.length($wanted-contrasts); |
| 68 | $wanted-idx: 1; | 72 | $wanted-idx: 1; |
| 69 | 73 | ||
| 70 | @while $colors-idx >= 1 and $colors-idx <= $colors-len and $wanted-idx <= $wanted-len { | 74 | @while $colors-idx <= $colors-len and $wanted-idx <= $wanted-len { |
| 71 | $color: nth($colors, $colors-idx); | 75 | $color: list.nth($colors, $colors-idx); |
| 72 | $lum: nth(blend-convert.lin_sRGB_to_XYZ(blend-convert.lin_sRGB(blend-convert.sassToRgb($color))), 2) + .05; | 76 | $lum: lum($color); |
| 73 | $contrast: math.div(math.max($base-lum, $lum), math.min($lum, $base-lum)); | 77 | $contrast: math.div(math.max($ref-lum, $lum), math.min($lum, $ref-lum)); |
| 74 | 78 | ||
| 75 | @if $contrast != 1 { | 79 | @if $contrast != 1 { |
| 76 | $contrast: $dir * $contrast; | 80 | @if $lum <= $ref-lum { |
| 77 | |||
| 78 | @if $lum <= $base-lum { | ||
| 79 | $contrast: -1 * $contrast; | 81 | $contrast: -1 * $contrast; |
| 80 | } | 82 | } |
| 81 | } | 83 | } |
| 82 | 84 | ||
| 83 | $wanted: nth($wanted-contrasts, $wanted-idx); | 85 | $wanted: list.nth($wanted-contrasts, $wanted-idx); |
| 84 | $wanted-key: nth($wanted, 1); | 86 | $wanted-key: list.nth($wanted, 1); |
| 85 | $wanted-contrast: nth($wanted, 2); | 87 | $wanted-contrast: list.nth($wanted, 2); |
| 86 | 88 | ||
| 87 | @if $contrast >= $wanted-contrast { | 89 | @if $contrast >= $wanted-contrast { |
| 88 | $result: map.set($result, $wanted-key, $color); | 90 | $result: map.set($result, $wanted-key, $color); |
| 89 | $wanted-idx: $wanted-idx + 1; | 91 | $wanted-idx: $wanted-idx + 1; |
| 90 | } @else { | 92 | } @else { |
| 91 | $colors-idx: $colors-idx + $dir * 1; | 93 | $colors-idx: $colors-idx + 1; |
| 92 | } | 94 | } |
| 93 | } | 95 | } |
| 94 | 96 | ||
| 95 | $last-color: nth($colors, if($dir == -1, 1, $colors-len)); | 97 | $last-color: list.nth($colors, $colors-len); |
| 96 | 98 | ||
| 97 | @if $wanted-idx <= $wanted-len { | 99 | @if $wanted-idx <= $wanted-len { |
| 98 | @for $i from $wanted-idx through $wanted-len { | 100 | @for $i from $wanted-idx through $wanted-len { |
| 99 | $wanted: nth($wanted-contrasts, $i); | 101 | $wanted: list.nth($wanted-contrasts, $i); |
| 100 | $wanted-key: nth($wanted, 1); | 102 | $wanted-key: list.nth($wanted, 1); |
| 101 | $result: map.set($result, $wanted-key, $last-color); | 103 | $result: map.set($result, $wanted-key, $last-color); |
| 102 | } | 104 | } |
| 103 | } | 105 | } |
| @@ -105,53 +107,21 @@ | |||
| 105 | @return $result; | 107 | @return $result; |
| 106 | } | 108 | } |
| 107 | 109 | ||
| 108 | @function gray-to-alpha($lightness, $color) { | 110 | @function palette($base-color, $contrasts, $reference-color: $base-color) { |
| 109 | @if $lightness >= 50% { | 111 | $palette: (); |
| 110 | @return rgba(#000, color.blackness($color)); | ||
| 111 | } @else { | ||
| 112 | @return rgba(#fff, color.whiteness($color)); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | 112 | ||
| 116 | @function color-palette($base, $dir: 1) { | 113 | @if list.nth(list.nth($contrasts, 1), 2) > list.nth(list.nth($contrasts, list.length($contrasts)), 2) { |
| 117 | @return ( | 114 | $contrasts: iro.fn-list-reverse($contrasts); |
| 118 | --solid: ( | ||
| 119 | --bg-hi: blend.scale($base, $lightness: $dir * 15%, $chroma: $dir * 7.5%), | ||
| 120 | --bg: $base, | ||
| 121 | --obj: blend.scale($base, $lightness: $dir * -15%, $chroma: $dir * -7.5%), | ||
| 122 | --obj-lo: blend.scale($base, $lightness: $dir * -30%, $chroma: $dir * -15%), | ||
| 123 | --fg: blend.contrast($base), | ||
| 124 | ), | ||
| 125 | --quiet: ( | ||
| 126 | --bg: rgba($base, .1), | ||
| 127 | --obj: rgba($base, .2), | ||
| 128 | --obj-lo: rgba($base, .4), | ||
| 129 | --fg-hi: rgba($base, .7), | ||
| 130 | --fg: blend.scale($base, $lightness: $dir * -30%, $chroma: $dir * -15%), | ||
| 131 | --fg-lo: blend.scale($base, $lightness: $dir * -45%, $chroma: $dir * -22.5%), | ||
| 132 | ), | ||
| 133 | --selection: rgba($base, .99), | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | |||
| 137 | @function gray-palette($lightness, $alpha: false) { | ||
| 138 | $grays: (); | ||
| 139 | |||
| 140 | @for $i from 0 through 100 { | ||
| 141 | $grays: append($grays, blend.lch($i * 1% 0 0)); | ||
| 142 | } | 115 | } |
| 143 | 116 | ||
| 144 | $colors: multi-contrast(blend.lch($lightness * 1% 0 0), $grays, config.$wanted-grays); | 117 | $i: -100%; |
| 145 | 118 | ||
| 146 | @each $key, $color in $colors { | 119 | @while $i <= 100% { |
| 147 | @if $alpha { | 120 | $palette: list.append($palette, blend.scale($base-color, $l: $i)); |
| 148 | $colors: map.set($colors, $key, gray-to-alpha($lightness, $color)); | 121 | $i: $i + config.$palette-precision; |
| 149 | } @else { | ||
| 150 | $colors: map.set($colors, $key, $color); | ||
| 151 | } | ||
| 152 | } | 122 | } |
| 153 | 123 | ||
| 154 | @return $colors; | 124 | @return multi-contrast($base-color, $palette, $contrasts, $reference-color); |
| 155 | } | 125 | } |
| 156 | 126 | ||
| 157 | @function px-to-em($size, $base: iro.$vars-root-size) { | 127 | @function px-to-em($size, $base: iro.$vars-root-size) { |
