|
| 1 | +/*! |
| 2 | + * Copyright (c) https://github.com/lutinglt |
| 3 | + * |
| 4 | + * See the NOTICE file distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import type { GitHubColor } from "./github"; |
| 21 | +import type { CodeMirrorColor, GitHubSyntax, PrettylightsColor } from "./githubSyntax"; |
| 22 | + |
| 23 | +type PrimerTokens = Record<string, { $value: string | { color: string } }>; |
| 24 | + |
| 25 | +function primerTokensGet(primerTokens: PrimerTokens): (key: string) => string { |
| 26 | + return (key: string) => { |
| 27 | + const entry = primerTokens[key]; |
| 28 | + if (typeof entry.$value === "object") return entry.$value.color; |
| 29 | + return entry.$value; |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +export function primer2GitHubColor(primerTokens: PrimerTokens, isDarkTheme: boolean = false): GitHubColor { |
| 34 | + // 安全获取色值: 从 $value 和 alpha 组合成最终 CSS 色值 |
| 35 | + const $ = primerTokensGet(primerTokens); |
| 36 | + |
| 37 | + return { |
| 38 | + isDarkTheme, |
| 39 | + avatar: { |
| 40 | + bgColor: $("avatar-bgColor"), |
| 41 | + borderColor: $("avatar-borderColor"), |
| 42 | + }, |
| 43 | + display: { |
| 44 | + blue: { fgColor: $("display-blue-fgColor") }, |
| 45 | + brown: { fgColor: $("display-brown-fgColor") }, |
| 46 | + cyan: { fgColor: $("display-cyan-fgColor") }, |
| 47 | + indigo: { fgColor: $("display-indigo-fgColor") }, |
| 48 | + lemon: { fgColor: $("display-lemon-fgColor") }, |
| 49 | + olive: { fgColor: $("display-olive-fgColor") }, |
| 50 | + teal: { fgColor: $("display-teal-fgColor") }, |
| 51 | + }, |
| 52 | + diffBlob: { |
| 53 | + addtionNum: { bgColor: $("diffBlob-additionNum-bgColor") }, |
| 54 | + addtionWord: { bgColor: $("diffBlob-additionWord-bgColor") }, |
| 55 | + deletionNum: { bgColor: $("diffBlob-deletionNum-bgColor") }, |
| 56 | + deletionWord: { bgColor: $("diffBlob-deletionWord-bgColor") }, |
| 57 | + hunkNum: { bgColor: { rest: $("diffBlob-hunkNum-bgColor-rest") } }, |
| 58 | + }, |
| 59 | + fgColor: { |
| 60 | + accent: $("fgColor-accent"), |
| 61 | + attention: $("fgColor-attention"), |
| 62 | + danger: $("fgColor-danger"), |
| 63 | + default: $("fgColor-default"), |
| 64 | + disabled: $("fgColor-disabled"), |
| 65 | + done: $("fgColor-done"), |
| 66 | + neutral: $("fgColor-neutral"), |
| 67 | + severe: $("fgColor-severe"), |
| 68 | + sponsors: $("fgColor-sponsors"), |
| 69 | + success: $("fgColor-success"), |
| 70 | + black: $("fgColor-black"), |
| 71 | + white: $("fgColor-white"), |
| 72 | + muted: $("fgColor-muted"), |
| 73 | + onEmphasis: $("fgColor-onEmphasis"), |
| 74 | + }, |
| 75 | + bgColor: { |
| 76 | + accent: { |
| 77 | + emphasis: $("bgColor-accent-emphasis"), |
| 78 | + muted: $("bgColor-accent-muted"), |
| 79 | + }, |
| 80 | + attention: { |
| 81 | + emphasis: $("bgColor-attention-emphasis"), |
| 82 | + muted: $("bgColor-attention-muted"), |
| 83 | + }, |
| 84 | + emphasis: $("bgColor-emphasis"), |
| 85 | + success: { |
| 86 | + emphasis: $("bgColor-success-emphasis"), |
| 87 | + muted: $("bgColor-success-muted"), |
| 88 | + }, |
| 89 | + danger: { |
| 90 | + emphasis: $("bgColor-danger-emphasis"), |
| 91 | + muted: $("bgColor-danger-muted"), |
| 92 | + }, |
| 93 | + done: { emphasis: $("bgColor-done-emphasis") }, |
| 94 | + default: $("bgColor-default"), |
| 95 | + inset: $("bgColor-inset"), |
| 96 | + muted: $("bgColor-muted"), |
| 97 | + neutral: { |
| 98 | + muted: $("bgColor-neutral-muted"), |
| 99 | + emphasis: $("bgColor-neutral-emphasis"), |
| 100 | + }, |
| 101 | + }, |
| 102 | + borderColor: { |
| 103 | + accent: { emphasis: $("borderColor-accent-emphasis") }, |
| 104 | + attention: { emphasis: $("borderColor-attention-emphasis") }, |
| 105 | + default: $("borderColor-default"), |
| 106 | + success: { emphasis: $("borderColor-success-emphasis") }, |
| 107 | + done: { emphasis: $("borderColor-done-emphasis") }, |
| 108 | + muted: $("borderColor-muted"), |
| 109 | + translucent: $("borderColor-translucent"), |
| 110 | + emphasis: $("borderColor-emphasis"), |
| 111 | + }, |
| 112 | + button: { |
| 113 | + primary: { |
| 114 | + fgColor: { |
| 115 | + accent: $("fgColor-success"), // 此为主题自定义值, 特殊绿色按钮颜色 |
| 116 | + rest: $("button-primary-fgColor-rest"), |
| 117 | + }, |
| 118 | + bgColor: { |
| 119 | + rest: $("button-primary-bgColor-rest"), |
| 120 | + hover: $("button-primary-bgColor-hover"), |
| 121 | + active: $("button-primary-bgColor-active"), |
| 122 | + }, |
| 123 | + }, |
| 124 | + danger: { |
| 125 | + fgColor: { |
| 126 | + rest: $("button-danger-fgColor-rest"), |
| 127 | + hover: $("button-danger-fgColor-hover"), |
| 128 | + }, |
| 129 | + bgColor: { |
| 130 | + hover: $("button-danger-bgColor-hover"), |
| 131 | + active: $("button-danger-bgColor-active"), |
| 132 | + }, |
| 133 | + }, |
| 134 | + star: { iconColor: $("button-star-iconColor") }, |
| 135 | + invisible: { iconColor: { rest: $("button-invisible-iconColor-rest") } }, |
| 136 | + }, |
| 137 | + control: { |
| 138 | + bgColor: { |
| 139 | + active: $("control-bgColor-active"), |
| 140 | + hover: $("control-bgColor-hover"), |
| 141 | + rest: $("control-bgColor-rest"), |
| 142 | + }, |
| 143 | + transparent: { |
| 144 | + bgColor: { |
| 145 | + active: $("control-transparent-bgColor-active"), |
| 146 | + hover: $("control-transparent-bgColor-hover"), |
| 147 | + selected: $("control-transparent-bgColor-selected"), |
| 148 | + }, |
| 149 | + }, |
| 150 | + danger: { bgColor: { active: $("control-danger-bgColor-active") } }, |
| 151 | + }, |
| 152 | + controlTrack: { bgColor: { rest: $("controlTrack-bgColor-rest") } }, |
| 153 | + controlKnob: { bgColor: { rest: $("controlKnob-bgColor-rest") } }, |
| 154 | + shadow: { |
| 155 | + floating: { small: isDarkTheme ? "#01040966" : "#25292e0a" }, // 特殊颜色, 因 Gitea 实现无法使用原始值, 提取对应值交给后续生成 |
| 156 | + inset: $("shadow-inset"), |
| 157 | + resting: { |
| 158 | + small: $("shadow-resting-small"), |
| 159 | + medium: isDarkTheme ? "#010409cc" : "#25292e1f", // 特殊颜色, 因 Gitea 实现无法使用原始值, 提取对应值交给后续生成 |
| 160 | + }, |
| 161 | + }, |
| 162 | + overlay: { |
| 163 | + backdrop: { bgColor: $("overlay-backdrop-bgColor") }, |
| 164 | + bgColor: $("overlay-bgColor"), |
| 165 | + }, |
| 166 | + underlineNav: { borderColor: { active: $("underlineNav-borderColor-active") } }, |
| 167 | + contribution: { |
| 168 | + default: { |
| 169 | + bgColor: { |
| 170 | + num0: $("contribution-default-bgColor-0"), |
| 171 | + num1: $("contribution-default-bgColor-1"), |
| 172 | + num2: $("contribution-default-bgColor-2"), |
| 173 | + num3: $("contribution-default-bgColor-3"), |
| 174 | + num4: $("contribution-default-bgColor-4"), |
| 175 | + }, |
| 176 | + borderColor: { num0: $("contribution-default-borderColor-0") }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }; |
| 180 | +} |
| 181 | +export function primer2GitHubSyntax(primerTokens: PrimerTokens): GitHubSyntax { |
| 182 | + const $ = primerTokensGet(primerTokens); |
| 183 | + |
| 184 | + const prettyLights: PrettylightsColor = { |
| 185 | + syntax: { |
| 186 | + brackethighlighter: { |
| 187 | + angle: $("color-prettylights-syntax-brackethighlighter-angle"), |
| 188 | + unmatched: $("color-prettylights-syntax-brackethighlighter-unmatched"), |
| 189 | + }, |
| 190 | + carriage: { |
| 191 | + return: { |
| 192 | + bg: $("color-prettylights-syntax-carriage-return-bg"), |
| 193 | + text: $("color-prettylights-syntax-carriage-return-text"), |
| 194 | + }, |
| 195 | + }, |
| 196 | + comment: $("color-prettylights-syntax-comment"), |
| 197 | + constant: $("color-prettylights-syntax-constant"), |
| 198 | + constantOtherReferenceLink: $("color-prettylights-syntax-constant-other-reference-link"), |
| 199 | + entity: $("color-prettylights-syntax-entity"), |
| 200 | + entityTag: $("color-prettylights-syntax-entity-tag"), |
| 201 | + invalid: { |
| 202 | + illegal: { |
| 203 | + bg: $("color-prettylights-syntax-invalid-illegal-bg"), |
| 204 | + text: $("color-prettylights-syntax-invalid-illegal-text"), |
| 205 | + }, |
| 206 | + }, |
| 207 | + keyword: $("color-prettylights-syntax-keyword"), |
| 208 | + markup: { |
| 209 | + bold: $("color-prettylights-syntax-markup-bold"), |
| 210 | + changed: { |
| 211 | + bg: $("color-prettylights-syntax-markup-changed-bg"), |
| 212 | + text: $("color-prettylights-syntax-markup-changed-text"), |
| 213 | + }, |
| 214 | + deleted: { |
| 215 | + bg: $("color-prettylights-syntax-markup-deleted-bg"), |
| 216 | + text: $("color-prettylights-syntax-markup-deleted-text"), |
| 217 | + }, |
| 218 | + heading: $("color-prettylights-syntax-markup-heading"), |
| 219 | + ignored: { |
| 220 | + bg: $("color-prettylights-syntax-markup-ignored-bg"), |
| 221 | + text: $("color-prettylights-syntax-markup-ignored-text"), |
| 222 | + }, |
| 223 | + inserted: { |
| 224 | + bg: $("color-prettylights-syntax-markup-inserted-bg"), |
| 225 | + text: $("color-prettylights-syntax-markup-inserted-text"), |
| 226 | + }, |
| 227 | + italic: $("color-prettylights-syntax-markup-italic"), |
| 228 | + list: $("color-prettylights-syntax-markup-list"), |
| 229 | + }, |
| 230 | + metaDiffRange: $("color-prettylights-syntax-meta-diff-range"), |
| 231 | + storageModifierImport: $("color-prettylights-syntax-storage-modifier-import"), |
| 232 | + string: $("color-prettylights-syntax-string"), |
| 233 | + stringRegexp: $("color-prettylights-syntax-string-regexp"), |
| 234 | + sublimelinterGutterMark: $("color-prettylights-syntax-sublimelinter-gutter-mark"), |
| 235 | + variable: $("color-prettylights-syntax-variable"), |
| 236 | + }, |
| 237 | + }; |
| 238 | + |
| 239 | + const codeMirror: CodeMirrorColor = { |
| 240 | + syntax: { |
| 241 | + fgColor: { |
| 242 | + comment: $("codeMirror-syntax-fgColor-comment"), |
| 243 | + constant: $("codeMirror-syntax-fgColor-constant"), |
| 244 | + entity: $("codeMirror-syntax-fgColor-entity"), |
| 245 | + keyword: $("codeMirror-syntax-fgColor-keyword"), |
| 246 | + storage: $("codeMirror-syntax-fgColor-storage"), |
| 247 | + string: $("codeMirror-syntax-fgColor-string"), |
| 248 | + support: $("codeMirror-syntax-fgColor-support"), |
| 249 | + variable: $("codeMirror-syntax-fgColor-variable"), |
| 250 | + }, |
| 251 | + }, |
| 252 | + matchingBracket: { |
| 253 | + fgColor: $("codeMirror-matchingBracket-fgColor"), |
| 254 | + }, |
| 255 | + }; |
| 256 | + |
| 257 | + return { prettyLights, codeMirror }; |
| 258 | +} |
| 259 | + |
| 260 | +import darkColorblind from "@primer/primitives/dist/styleLint/functional/themes/dark-colorblind.json" with { type: "json" }; |
| 261 | +import darkDimmed from "@primer/primitives/dist/styleLint/functional/themes/dark-dimmed.json" with { type: "json" }; |
| 262 | +import darkTritanopia from "@primer/primitives/dist/styleLint/functional/themes/dark-tritanopia.json" with { type: "json" }; |
| 263 | +import dark from "@primer/primitives/dist/styleLint/functional/themes/dark.json" with { type: "json" }; |
| 264 | +import lightColorblind from "@primer/primitives/dist/styleLint/functional/themes/light-colorblind.json" with { type: "json" }; |
| 265 | +import lightTritanopia from "@primer/primitives/dist/styleLint/functional/themes/light-tritanopia.json" with { type: "json" }; |
| 266 | +import light from "@primer/primitives/dist/styleLint/functional/themes/light.json" with { type: "json" }; |
| 267 | + |
| 268 | +export default { |
| 269 | + dark, |
| 270 | + darkColorblind, |
| 271 | + darkDimmed, |
| 272 | + darkTritanopia, |
| 273 | + light, |
| 274 | + lightColorblind, |
| 275 | + lightTritanopia, |
| 276 | +}; |
0 commit comments