function __tide_apply_terminal_appearance --argument-names appearance
    contains -- $appearance dark light
    or return 2

    if test "$__tide_terminal_appearance" = "$appearance"
        return 1
    end

    if not set --query __tide_terminal_color_names
        set --global __tide_terminal_color_names
        set --global __tide_terminal_dark_colors

        for name in (set --universal --names | string match -e -r '^tide_' | sort)
            string match -q 'tide_*color*' -- $name
            or continue

            set -l current_value $$name
            test (count $current_value) -eq 1
            or continue
            string match -qr '^[0-9A-Fa-f]{6}$' -- $current_value
            or continue

            set --append __tide_terminal_color_names $name
            set --append __tide_terminal_dark_colors $current_value
        end
    end

    test (count $__tide_terminal_color_names) -gt 0
    or return 2

    for index in (seq (count $__tide_terminal_color_names))
        set -l name $__tide_terminal_color_names[$index]
        set -l color $__tide_terminal_dark_colors[$index]
        if test "$appearance" = light
            set color (__tide_terminal_light_color $color)
        end
        set --global --export $name $color
    end

    set --global __tide_terminal_appearance $appearance
end
