蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线 Chromium 实测 Bootstrap 5 语义文本色、50% 透明文本、链接悬停及背景色配对规则

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-colors.py
explore-colors.py208 行
"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 06 (colors)."""
import json
from playwright.sync_api import sync_playwright

BS5_CSS = """
:root {
  --bs-primary-rgb: 13, 110, 253;
  --bs-secondary-rgb: 108, 117, 125;
  --bs-success-rgb: 25, 135, 84;
  --bs-info-rgb: 13, 202, 240;
  --bs-warning-rgb: 255, 193, 7;
  --bs-danger-rgb: 220, 53, 69;
  --bs-light-rgb: 248, 249, 250;
  --bs-dark-rgb: 33, 37, 41;
  --bs-white-rgb: 255, 255, 255;
  --bs-body-color-rgb: 33, 37, 41;
  --bs-text-opacity: 1;
  --bs-bg-opacity: 1;
}
body { margin: 0; color: #212529; font-family: system-ui, sans-serif; }
.text-muted { color: #6c757d !important; }
.text-primary { color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important; }
.text-secondary { color: rgba(var(--bs-secondary-rgb), var(--bs-text-opacity)) !important; }
.text-success { color: rgba(var(--bs-success-rgb), var(--bs-text-opacity)) !important; }
.text-info { color: rgba(var(--bs-info-rgb), var(--bs-text-opacity)) !important; }
.text-warning { color: rgba(var(--bs-warning-rgb), var(--bs-text-opacity)) !important; }
.text-danger { color: rgba(var(--bs-danger-rgb), var(--bs-text-opacity)) !important; }
.text-light { color: rgba(var(--bs-light-rgb), var(--bs-text-opacity)) !important; }
.text-dark { color: rgba(var(--bs-dark-rgb), var(--bs-text-opacity)) !important; }
.text-body { color: rgba(var(--bs-body-color-rgb), var(--bs-text-opacity)) !important; }
.text-white { color: rgba(var(--bs-white-rgb), var(--bs-text-opacity)) !important; }
.text-black-50 { color: rgba(0, 0, 0, 0.5) !important; }
.text-white-50 { color: rgba(255, 255, 255, 0.5) !important; }
.link-primary { color: #0d6efd; }
.link-primary:hover { color: #0a58ca; }
.bg-primary { background-color: rgba(var(--bs-primary-rgb), var(--bs-bg-opacity)) !important; }
.bg-secondary { background-color: rgba(var(--bs-secondary-rgb), var(--bs-bg-opacity)) !important; }
.bg-success { background-color: rgba(var(--bs-success-rgb), var(--bs-bg-opacity)) !important; }
.bg-info { background-color: rgba(var(--bs-info-rgb), var(--bs-bg-opacity)) !important; }
.bg-warning { background-color: rgba(var(--bs-warning-rgb), var(--bs-bg-opacity)) !important; }
.bg-danger { background-color: rgba(var(--bs-danger-rgb), var(--bs-bg-opacity)) !important; }
.bg-light { background-color: rgba(var(--bs-light-rgb), var(--bs-bg-opacity)) !important; }
.bg-dark { background-color: rgba(var(--bs-dark-rgb), var(--bs-bg-opacity)) !important; }
.text-bg-primary { color: #fff !important; background-color: RGBA(13, 110, 253, var(--bs-bg-opacity, 1)) !important; }
.text-bg-warning { color: #000 !important; background-color: RGBA(255, 193, 7, var(--bs-bg-opacity, 1)) !important; }
.text-bg-dark { color: #fff !important; background-color: RGBA(33, 37, 41, var(--bs-bg-opacity, 1)) !important; }
"""

HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
  <div id="text-list">
    <p data-k="muted" class="text-muted">muted</p>
    <p data-k="primary" class="text-primary">primary</p>
    <p data-k="success" class="text-success">success</p>
    <p data-k="info" class="text-info">info</p>
    <p data-k="warning" class="text-warning">warning</p>
    <p data-k="danger" class="text-danger">danger</p>
    <p data-k="secondary" class="text-secondary">secondary</p>
    <p data-k="dark" class="text-dark">dark</p>
    <p data-k="body" class="text-body">body</p>
    <p data-k="light" class="text-light">light</p>
    <p data-k="white" class="text-white">white</p>
  </div>
  <p id="op-black" class="text-black-50">black 50</p>
  <p id="op-white" class="text-white-50 bg-dark">white 50</p>
  <a id="a-danger" href="#d" class="text-danger">danger link</a>
  <a id="a-link-pri" href="#p" class="link-primary">bs5 link-primary</a>
  <div id="bg-list">
    <p id="bg-only-primary" class="bg-primary">bg only</p>
    <p id="bg-pair-primary" class="bg-primary text-white">bg + text-white</p>
    <p id="bg-pair-light" class="bg-light text-dark">bg-light + text-dark</p>
    <p id="bg-success" class="bg-success text-white">success</p>
    <p id="bg-info" class="bg-info text-white">info</p>
    <p id="bg-warning" class="bg-warning text-white">warning</p>
    <p id="bg-danger" class="bg-danger text-white">danger</p>
    <p id="bg-secondary" class="bg-secondary text-white">secondary</p>
    <p id="bg-dark" class="bg-dark text-white">dark</p>
    <p id="tbg-primary" class="text-bg-primary">text-bg-primary</p>
    <p id="tbg-warning" class="text-bg-warning">text-bg-warning</p>
  </div>
</body>
</html>
"""


def launch_browser(pw):
    try:
        return pw.chromium.launch(headless=True)
    except Exception:
        return pw.chromium.launch(headless=True, channel="chrome")


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 1000, "height": 800})
        page.set_content(HTML_DOC)

        # Case 1: All 11 semantic text color classes
        c1 = page.evaluate("""() => {
            const out = {};
            for (const el of document.querySelectorAll('#text-list p')) {
                out[el.dataset.k] = getComputedStyle(el).color;
            }
            return out;
        }""")
        exp1 = {
            "muted": "rgb(108, 117, 125)",
            "primary": "rgb(13, 110, 253)",
            "success": "rgb(25, 135, 84)",
            "info": "rgb(13, 202, 240)",
            "warning": "rgb(255, 193, 7)",
            "danger": "rgb(220, 53, 69)",
            "secondary": "rgb(108, 117, 125)",
            "dark": "rgb(33, 37, 41)",
            "body": "rgb(33, 37, 41)",
            "light": "rgb(248, 249, 250)",
            "white": "rgb(255, 255, 255)",
        }
        cases.append({
            "name": "semantic_text_colors",
            "passed": c1 == exp1,
            "expected": exp1,
            "actual": c1,
        })

        # Case 2: 50% opacity text classes
        c2 = page.evaluate("""() => ({
            black_50: getComputedStyle(document.getElementById('op-black')).color,
            white_50_color: getComputedStyle(document.getElementById('op-white')).color,
            white_50_bg: getComputedStyle(document.getElementById('op-white')).backgroundColor
        })""")
        exp2 = {
            "black_50": "rgba(0, 0, 0, 0.5)",
            "white_50_color": "rgba(255, 255, 255, 0.5)",
            "white_50_bg": "rgb(33, 37, 41)",
        }
        cases.append({
            "name": "text_opacity_50",
            "passed": c2 == exp2,
            "expected": exp2,
            "actual": c2,
        })

        # Case 3: Colored links & .link-primary hover darkening
        danger_col = page.eval_on_selector("#a-danger", "el => getComputedStyle(el).color")
        link_before = page.eval_on_selector("#a-link-pri", "el => getComputedStyle(el).color")
        page.hover("#a-link-pri")
        link_hover = page.eval_on_selector("#a-link-pri", "el => getComputedStyle(el).color")
        c3 = {
            "text_danger_link": danger_col,
            "link_primary_default": link_before,
            "link_primary_hover": link_hover,
        }
        exp3 = {
            "text_danger_link": "rgb(220, 53, 69)",
            "link_primary_default": "rgb(13, 110, 253)",
            "link_primary_hover": "rgb(10, 88, 202)",
        }
        cases.append({
            "name": "link_text_colors_and_hover",
            "passed": c3 == exp3,
            "expected": exp3,
            "actual": c3,
        })

        # Case 4: Background colors do not alter text color vs .text-bg-* helper
        c4 = page.evaluate("""() => {
            const pick = id => {
                const cs = getComputedStyle(document.getElementById(id));
                return { bg: cs.backgroundColor, color: cs.color };
            };
            return {
                bg_only_primary: pick('bg-only-primary'),
                bg_pair_primary: pick('bg-pair-primary'),
                bg_pair_light: pick('bg-pair-light'),
                bg_danger: pick('bg-danger'),
                bg_dark: pick('bg-dark'),
                text_bg_primary: pick('tbg-primary'),
                text_bg_warning: pick('tbg-warning')
            };
        }""")
        exp4 = {
            "bg_only_primary": {"bg": "rgb(13, 110, 253)", "color": "rgb(33, 37, 41)"},
            "bg_pair_primary": {"bg": "rgb(13, 110, 253)", "color": "rgb(255, 255, 255)"},
            "bg_pair_light": {"bg": "rgb(248, 249, 250)", "color": "rgb(33, 37, 41)"},
            "bg_danger": {"bg": "rgb(220, 53, 69)", "color": "rgb(255, 255, 255)"},
            "bg_dark": {"bg": "rgb(33, 37, 41)", "color": "rgb(255, 255, 255)"},
            "text_bg_primary": {"bg": "rgb(13, 110, 253)", "color": "rgb(255, 255, 255)"},
            "text_bg_warning": {"bg": "rgb(255, 193, 7)", "color": "rgb(0, 0, 0)"},
        }
        cases.append({
            "name": "background_colors_and_text_bg",
            "passed": c4 == exp4,
            "expected": exp4,
            "actual": c4,
        })

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/bootstrap5-colors-lab/explore-colors.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-colors.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a707.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习