Skip to content

Commit ba4c351

Browse files
authored
Update common build scripts (#77)
- Rebase common build scripts from yaru - Update meson build scripts for gtk and metacity - Update version in meson build script - Update test script
1 parent e21fd99 commit ba4c351

9 files changed

+219
-31
lines changed

common/accent-colors.scss.in

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import 'sass-utils';
2+
13
@function get_accent_color($accent_color, $is_dark: false) {
24
$color: null;
35
@if $accent_color == 'default' {
@@ -30,9 +32,19 @@
3032
}
3133

3234
$sucharu_is_dark_variant: @sucharu_dark_variant@;
35+
$jet: #181818;
36+
$sucharu_bg_color: if($sucharu_is_dark_variant, #FAFAFA, lighten($jet, 8%));
3337
$sucharu_accent_bg_color: get_accent_color('@sucharu_accent_color@', $sucharu_is_dark_variant);
34-
$accent_bg_color: $sucharu_accent_bg_color;
35-
$accent_color: $sucharu_accent_bg_color;
38+
39+
$contrast_target: if($sucharu_is_dark_variant, 4.5, 4.8);
40+
$sucharu_accent_color: optimize-contrast($sucharu_bg_color,
41+
$sucharu_accent_bg_color, $target: $contrast_target);
42+
3643
@debug("Accent color is " + $sucharu_accent_bg_color);
44+
@debug("Contrast optimized accent is " + $sucharu_accent_color);
45+
46+
$accent_bg_color: $sucharu_accent_bg_color;
47+
$accent_fg_color: white;
48+
$accent_color: $sucharu_accent_color;
3749

3850
@import '@sucharu_theme_entry_point@'

common/colorize-dummy-svg.py

+12
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,29 @@
2424

2525
# Keep this in sync with sucharu-colors-defs.scss, or the input CSS in use.
2626
DUMMY_COLORS = {
27+
'accent-color': '#00ff03',
28+
'accent-color-hc': '#00ff04',
2729
'accent-bg-color': '#00ff01',
30+
'accent-bg-color-hc': '#00ff05',
2831
'accent-active-color': '#00ff02',
32+
'accent-active-color-hc': '#00ff06',
2933
'accent-border-color': '#ff0001',
34+
'accent-border-color-hc': '#ff0002',
3035
'accent-focused-color': '#0101ff',
36+
'accent-focused-color-hc': '#0101f1',
3137
'bg-color': '#ffff00',
3238
'border-color': '#ff00ff',
39+
'border-color-hc': '#ff00f1',
3340
'disabled-bg-color': '#ffff02',
41+
'disabled-bg-color-hc': '#ffff04',
3442
'switch-bg-color': '#ffff01',
43+
'switch-bg-color-hc': '#ffff05',
3544
'check-bg-color': '#ffff03',
45+
'check-bg-color-hc': '#ffff06',
3646
}
3747

48+
assert len(set(DUMMY_COLORS.values())) == len(DUMMY_COLORS.values())
49+
3850
def read_colors_replacements(css_file):
3951
colors_replacements = {}
4052

common/sass-utils.scss

+124-15
Original file line numberDiff line numberDiff line change
@@ -63,41 +63,150 @@
6363
}
6464

6565
@function list-length($list) {
66+
@return length($list);
67+
}
68+
69+
@function list-nth($list, $nth) {
70+
$length: length($list);
71+
@if ($length == 0 or abs($nth) > $length) {
72+
@return null;
73+
}
74+
75+
@if ($nth >= 0) {
76+
$nth: $nth + 1;
77+
}
78+
79+
@return nth($list, $nth);
80+
}
81+
82+
@function list-index($list, $item) {
6683
$i: 0;
6784
@each $e in $list {
85+
@if ($e == $item) {
86+
@return $i;
87+
}
88+
6889
$i: $i + 1;
6990
}
7091

71-
@return $i;
92+
@return null;
7293
}
7394

74-
@function list-nth($list, $nth) {
75-
$i: 0;
95+
@function pow($base, $exponent, $root: 1) {
96+
@if ($exponent == 0) {
97+
@return 1;
98+
}
7699

77-
@if $nth < 0 {
78-
$nth: list-length($list) + $nth;
100+
@if ($base == 0) {
101+
@return 0;
79102
}
80103

81-
@each $e in $list {
82-
@if ($i == $nth) {
83-
@return $e;
104+
@if ($root != 1) {
105+
@if ($exponent == $root) {
106+
@return $base;
84107
}
108+
$base: nth-root($base, $root);
109+
}
85110

111+
@if $exponent < 0 {
112+
$base: 1 / $base;
113+
$exponent: -$exponent;
114+
} @else if ($exponent % 2 == 0) {
115+
$half-pow: pow($base, $exponent / 2);
116+
@return $half-pow * $half-pow;
117+
}
118+
119+
$i: 1;
120+
$val: $base;
121+
@while ($i < $exponent) {
122+
$val: $val * $base;
86123
$i: $i + 1;
87124
}
88125

89-
@return null;
126+
@return $val;
90127
}
91128

92-
@function list-index($list, $item) {
93-
$i: 0;
94-
@each $e in $list {
95-
@if ($e == $item) {
96-
@return $i;
129+
@function nth-root($value, $n, $max_iterations: 100) {
130+
@if ($n <= 0) {
131+
@error "Not supported"
132+
}
133+
134+
@if ($n == 1 or $value == 0) {
135+
@return $value;
136+
}
137+
138+
$i: 1;
139+
$pre-val: 1;
140+
@while ($i < $max_iterations) {
141+
$val: (1.0 / $n) * ((($n - 1) * $pre-val) + $value / pow($pre-val, $n - 1));
142+
143+
@if ($pre-val == $val) {
144+
@return $val;
97145
}
98146

147+
$pre-val: $val;
99148
$i: $i + 1;
100149
}
101150

102-
@return null;
151+
@error ("Failed to compute " + $n + "th root of " + $value + " in " +
152+
$max_iterations + " iterations");
153+
}
154+
155+
@function truncate($value, $decimals: 10) {
156+
@if ($decimals < 0) {
157+
@error "Not supported"
158+
}
159+
160+
$multiplier: pow(10, $decimals);
161+
@return floor($value * $multiplier) / $multiplier;
162+
}
163+
164+
// Credits to https://css-tricks.com/snippets/sass/luminance-color-function/
165+
@function luminance($color) {
166+
$colors: (
167+
'red': red($color),
168+
'green': green($color),
169+
'blue': blue($color)
170+
);
171+
172+
@each $name, $value in $colors {
173+
$adjusted: 0;
174+
$value: $value / 255;
175+
176+
@if $value < 0.03928 {
177+
$value: $value / 12.92;
178+
} @else {
179+
$value: ($value + .055) / 1.055;
180+
$value: pow($value, 12, 5);
181+
}
182+
183+
$colors: map-merge($colors, ($name: $value));
184+
}
185+
186+
@return ((map-get($colors, 'red') * .2126) +
187+
(map-get($colors, 'green') * .7152) +
188+
(map-get($colors, 'blue') * .0722));
189+
}
190+
191+
@function color-contrast($color1, $color2) {
192+
$c1-luminance: luminance($color1);
193+
$c2-luminance: luminance($color2);
194+
195+
$lighter-luminance: max($c1-luminance, $c2-luminance);
196+
$darker-luminance: min($c1-luminance, $c2-luminance);
197+
198+
@return ($lighter-luminance + 0.05) / ($darker-luminance + 0.05);
199+
}
200+
201+
@function optimize-contrast($bg, $fg, $large-text: false, $target: 4.5) {
202+
@if ($large-text and $target == 4.5) {
203+
$target: 3;
204+
}
205+
206+
$dark-bg: luminance($fg) > luminance($bg);
207+
@while (color-contrast($bg, $fg) < $target) {
208+
$fg: if($dark-bg, lighten($fg, 0.1), darken($fg, 0.1));
209+
}
210+
211+
@return $fg;
103212
}

common/sucharu-colors-defs.scss

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
// assets (dummy) colors via colorize-dummy-svg.py
33

44
$variant: if($sucharu_is_dark_variant, 'dark', 'light');
5+
$hc_contrast_target: 5.5;
56

67
@import 'colors';
78

89
$sucharu_colors: (
910
bg-color: $bg_color,
11+
accent-color: $sucharu_accent_color,
1012
accent-bg-color: $sucharu_accent_bg_color,
11-
accent-active-color: if($variant == 'light', darken($accent_bg_color, 15%), darken($accent_bg_color, 20%)),
12-
accent-focused-color: if($variant == 'light', lighten($accent_bg_color, 10%), lighten($accent_bg_color, 7%)),
13+
accent-active-color: if($variant == 'dark', darken($accent_bg_color, 20%), darken($accent_bg_color, 15%)),
14+
accent-focused-color: if($variant == 'dark', darken($accent_bg_color, 7%), darken($accent_bg_color, 3%)),
1315
border-color: $borders_color,
1416
disabled-bg-color: mix($bg_color, $fg_color, 80%),
15-
switch-bg-color: if($variant == 'light', lighten($ash, 20%), lighten($inkstone, 5%)),
16-
check-bg-color: if($variant == 'light', $porcelain, lighten($bg_color, 2%)),
17+
switch-bg-color: if($variant == 'dark', lighten($inkstone, 10%), lighten($ash, 20%)),
18+
check-bg-color: if($variant == 'dark', lighten($bg_color, 2%), $porcelain),
1719
);
1820

1921
$sucharu_colors: map-merge($sucharu_colors, (
20-
accent-border-color: lighten(map-get($sucharu_colors, accent-active-color), 35%),
22+
accent-border-color: lighten(map-get($sucharu_colors, accent-active-color), 17%),
2123
));
2224

2325
@function opaque-color($color, $background: $bg_color) {
@@ -39,5 +41,11 @@ $sucharu_colors: map-merge($sucharu_colors, (
3941
@if $opaque != $color {
4042
-sucharu-#{$name}-opaque: $opaque;
4143
}
44+
45+
@if $name != "bg-color" and not map-has-key($sucharu_colors, $name + "-hc") and
46+
not str-ends-with($name, "-hc") {
47+
-sucharu-#{$name}-hc: optimize-contrast($bg_color,
48+
$color, $target: $hc_contrast_target);
49+
}
4250
}
4351
};

common/test-sass-utils.scss

+46-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
@import 'sass-utils';
1919

2020
@function assert($result, $expected: true) {
21-
@if $result != $expected {
21+
@if (type-of($result) == 'color' and type-of($expected) == 'color') {
22+
$result: #{$result};
23+
$expected: #{$expected};
24+
}
25+
26+
@if ($result != $expected or type-of($result) != type-of($expected)) {
2227
$result: if($result == null, 'null', $result);
2328
$expected: if($expected == null, 'null', $expected);
2429
@error "Assertion failed, expected '" + $expected + "', got '" + $result + "'";
@@ -96,3 +101,43 @@
96101
@debug test('list-index, Three, valid', list-index([1, '2', false], 1), 0);
97102
@debug test('list-index, Three, valid', list-index([1, '2', false], '2'), 1);
98103
@debug test('list-index, Three, valid', list-index([1, '2', false], false), 2);
104+
105+
@debug test('nth-root 1-root of 0', nth-root(0, 1), 0);
106+
@debug test('nth-root 0-root of 0', nth-root(0, 20), 0);
107+
@debug test('nth-root 2-root of 4', nth-root(4, 2), 2);
108+
@debug test('nth-root 4-root of 256', nth-root(256, 4), 4);
109+
@debug test('nth-root 5-root of 4096', nth-root(4096.0, 5), 5.278031643091578);
110+
111+
@debug test('pow 0^0', pow(0, 0), 1);
112+
@debug test('pow 1^0', pow(1, 0), 1);
113+
@debug test('pow 10^0', pow(10, 0), 1);
114+
115+
@debug test('pow 0^10', pow(0, 10), 0);
116+
@debug test('pow 1^20', pow(1, 20), 1);
117+
@debug test('pow 2^8', pow(2, 8), 256);
118+
@debug test('pow 10^3', pow(10, 3), 1000);
119+
120+
@debug test('pow 2^-2', pow(2, -2), 1/4);
121+
@debug test('pow 3^-3', pow(3, -3), 1/27);
122+
123+
@debug test('pow 2^(2/2)', pow(2, 2, 2), 2);
124+
@debug test('pow 2^(4/2)', pow(2, 4, 2), 4);
125+
@debug test('pow 2^(12/5)', pow(2, 12, 5), 5.278031643091582);
126+
127+
@debug test('truncate 0.1234567890123456789', truncate(0.123456789123456789), 0.1234567891);
128+
@debug test('truncate 5 0.12345678901234', truncate(0.12345678901234, 5), 0.12345);
129+
@debug test('truncate 5 0.12345678901234', truncate(0.12345678901234, 1), 0.1);
130+
@debug test('truncate 5 5.12345678901234', truncate(5.12345678901234, 0), 5);
131+
132+
@debug test('luminance white', luminance(white), 1);
133+
@debug test('luminance black', luminance(black), 0);
134+
@debug test('luminance red', luminance(#f00), 0.2126);
135+
@debug test('luminance green', luminance(#0f0), 0.7152);
136+
@debug test('luminance blue', luminance(#00f), 0.0722);
137+
@debug test('luminance Ubuntu orange', truncate(luminance(#dd4814), 6), 0.200573);
138+
139+
@debug test('color contrast black/white', color-contrast(white, black), 21);
140+
@debug test('color contrast blue/blue', color-contrast(blue, blue), 1);
141+
142+
@debug test('optimize-contrast black/blue', optimize-contrast(black, blue), #5e5eff);
143+
@debug test('optimize-contrast white/blue', optimize-contrast(white, lightgray), #767676);

gtk/src/meson.build

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ gtk_versions = [
99
]
1010

1111
gtk_sucharu_colors_defs = {}
12-
12+
sass_global_paths = ['-I', meson.project_source_root() / 'common']
1313
message('\n\nAll flavours: ')
1414
message(flavours)
1515

1616
foreach flavour: flavours
17+
message('Configuring flavour ' + flavour)
1718
suffix = flavour == 'default' ? '' : '-@0@'.format(flavour)
1819
theme_name = meson.project_name() + suffix
1920
is_dark = flavour == 'dark' or flavour.endswith('-dark')
@@ -46,6 +47,7 @@ foreach flavour: flavours
4647
)
4748

4849
accent_color_code = run_command(sassc,
50+
sass_global_paths,
4951
accent_info_css, check: true).stderr().split(' Accent color is ')[1].strip()
5052
assert(accent_color_code.startswith('#'), 'No accent color code found')
5153
endif
@@ -138,7 +140,7 @@ foreach flavour: flavours
138140

139141
# Look for scss files in the variant dir first, otherwise fallback to
140142
# base, default or upstream paths, so using reversed order
141-
scss_paths = []
143+
scss_paths = sass_global_paths
142144
gtk_scss_dependencies = []
143145
foreach src: sources_priority
144146
scss_paths += ['-I', meson.current_source_dir() / src ]
@@ -208,7 +210,7 @@ foreach flavour: flavours
208210
)
209211
endforeach
210212
install_data(gtk_css, install_dir: install_path)
211-
213+
212214
# build and install assets
213215
foreach src: sources_priority
214216
assets_rel_dir = src / 'assets'

meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project('Sucharu',
2-
version: '2.0.8',
2+
version: '2.0.9',
33
meson_version: '>= 0.60',
44
license : ['GPL3', 'CC BY-SA 4.0'],
55
default_options: ['prefix=/usr'])

metacity/src/meson.build

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ foreach flavour: flavours
3030
# install metacity assets
3131
metacity_asset_dir = join_paths(flavour, 'metacity-1')
3232
metacity_svg_data = run_command(
33-
'find', metacity_asset_dir, '-name', '*.svg'
34-
).stdout().strip().split('\n')
33+
'find', metacity_asset_dir, '-name', '*.svg',
34+
check: true).stdout().strip().split('\n')
3535

3636
metacity_assets_dest = join_paths(theme_dir, 'metacity-1')
3737
install_data(metacity_svg_data, install_dir: metacity_assets_dest)

test-sucharu

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
rm -rf ~/.local/share/themes/Sucharu*
44

55
meson setup -Dprefix=$HOME/.local builddir
6-
meson compile -C builddir
7-
meson install -C builddir
6+
meson compile -C builddir --verbose
7+
meson install -C builddir # --dry-run
88

99
# remove build directory
1010
rm -rf builddir

0 commit comments

Comments
 (0)