Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22418440
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/builders/colors-builder.ts b/src/builders/colors-builder.ts
index 938de38..6d280e3 100644
--- a/src/builders/colors-builder.ts
+++ b/src/builders/colors-builder.ts
@@ -1,216 +1,248 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { build } from "esbuild";
import tempfile from "tempfile";
import { promises as fs } from "fs";
import { resolve } from "path";
import { colord } from "colord";
import { formatWithPrettier } from "../utils/prettier.js";
import { Builder } from "./builder.js";
import { exec } from "../utils/exec.js";
export const COLORS_TS_PATH = "src/back/colors.ts"; // keeping it in src/back instead of just src to make typescript hints work there
export const COLORS_CSS_PATH = "src/colors.css";
export const COLORS_HTML_PATH = "src/colors.html";
export class ColorsBuilder extends Builder {
getName(): string {
return "colors";
}
ownsFile(file_path: string) {
return file_path == COLORS_TS_PATH;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async dispose(): Promise<void> {}
async _build() {
const outfile = tempfile({ extension: "mjs" });
await build({
entryPoints: [COLORS_TS_PATH],
bundle: true,
format: "esm",
outfile,
});
- await fs.appendFile(outfile, "console.log(JSON.stringify(colors))");
+ await fs.appendFile(
+ outfile,
+ /* HTML */ `<script>
+ console.log(
+ JSON.stringify(
+ Object.fromEntries(
+ Object.entries(colors).map(
+ ([group_name, group]) => [
+ group_name,
+ Object.fromEntries(
+ Object.entries(group).map(
+ ([color_name, color]) => {
+ const shades = [];
+ for (let i = 0; i <= 9; i++) {
+ shades.push(
+ shade(color, i)
+ );
+ }
+ return [
+ color_name,
+ { color, shades },
+ ];
+ }
+ )
+ ),
+ ]
+ )
+ )
+ )
+ );
+ </script>`.replaceAll(/<\/?script>/g, "") // <script> helps Prettier format this snippet
+ );
const output = await exec("node", [outfile]);
- const colors = JSON.parse(output.stdout) as Record<
+ const color_groups = JSON.parse(output.stdout) as Record<
string,
- Record<string, string>
+ Record<string, { color: string; shades: string[] }>
>;
let css_colors = "";
let html_colors = "";
- for (const group_name of Object.keys(colors).sort((key) =>
- key == "basic" ? 1 : -1
+
+ function html_color_box(
+ color: string,
+ shade_no: string,
+ is_main = false
+ ) {
+ return /* HTML */ `<div
+ class="color-box ${colord(color).isDark()
+ ? "is-dark"
+ : ""} ${is_main ? "is-main" : ""}"
+ style="background-color: ${color}; order: ${parseInt(shade_no)}"
+ data-color-name="${shade_no}"
+ ></div>`;
+ }
+
+ for (const group_name of Object.keys(color_groups).sort((key) =>
+ key == "brand" ? -1 : 11
)) {
- const sorted_colors = Object.entries(colors[group_name]).sort(
- ([key1], [key2]) => (key1 > key2 ? 1 : -1)
- );
- html_colors += `<h2>${group_name}</h2><div class="group"><div class="base">`;
- let last_color_name_base = "";
- for (const [color_name, color] of sorted_colors) {
- const color_name_elements = color_name.split("-");
- const color_name_base = color_name_elements
- .filter((s) => !/^(light|dark)[0-9]$/.test(s))
- .join("-");
- let index = 0;
- const modifier = color_name_elements.at(-1);
- let multiplier = 1;
- let steps = 0;
- if (modifier) {
- if (modifier.startsWith("dark")) {
- multiplier = -1;
+ html_colors += `<h2>${group_name}</h2><div class="group">`;
+ const colors = color_groups[group_name];
+ for (const [color_name, color_info] of Object.entries(colors)) {
+ html_colors += `<h3>${color_name}</h3><div class="shades">`;
+ const main_color = color_info.color;
+ css_colors += `--color-${group_name}-${color_name}: ${main_color};`;
+ const main_l = colord(main_color).toHsl().l;
+ html_colors += html_color_box(
+ main_color,
+ Math.round((main_l - (main_l % 10)) / 10)
+ .toString()
+ .padStart(2, "0"),
+ true
+ );
+ color_info.shades.forEach((shaded_color, index) => {
+ if (
+ Math.abs(
+ colord(shaded_color).toHsl().l -
+ colord(main_color).toHsl().l
+ ) < 10
+ ) {
+ return;
}
- }
- if (
- modifier &&
- modifier.match(/[0-9]$/) &&
- modifier.match(/^(dark|light)[0-9]/)
- ) {
- steps = parseInt(modifier.at(-1)!);
- }
- index = multiplier * steps;
- const is_new_base = color_name_base != last_color_name_base;
- css_colors += `--color-${group_name}-${color_name}: ${color};`;
- if (is_new_base) {
- html_colors += `</div><h3>${color_name_base}</h3><div class="base" base="${color_name_base}">`;
- }
- if (color != "#000000" && color != "#ffffff") {
- html_colors += /* HTML */ `<div
- class="color-box ${colord(color).isDark()
- ? "is-dark"
- : ""} ${index == 0 ? "is-main" : ""}"
- style="background-color: ${color}; order: ${index}"
- data-color-name="${color_name}"
- data-color-group="${group_name}"
- ></div>`;
- }
- last_color_name_base = color_name_base;
+ const shade_no = index.toString().padStart(2, "0");
+ css_colors += `--color-${group_name}-${color_name}-shade${shade_no}: ${shaded_color};`;
+ html_colors += html_color_box(shaded_color, shade_no);
+ });
+ html_colors += `</div>`;
}
- html_colors += "</div></div>"; // close group and base
+ html_colors += "</div>";
}
const css = await formatWithPrettier(
`/* DO NOT EDIT! This file is automatically generated by sealgen */
:root {
${css_colors}
}`,
"css"
);
const html = await formatWithPrettier(
/* HTML */ `<!DOCTYPE html>
<html>
<head>
<style>
* {
font-family: sans-serif;
}
- .base {
+ .shades {
display: flex;
}
.color-box {
width: 100px;
height: 100px;
transition: all 50ms;
transition-timing-function: ease-in-out;
cursor: pointer;
&:hover {
transform: scale(1.1);
}
&.is-main {
height: 116px;
width: 116px;
margin-top: -8px;
box-shadow: 0px 0px 6px 2px white;
z-index: 1;
}
}
.color-box:after {
content: attr(data-color-name);
+ box-sizing: border-box;
display: block;
color: black;
font-size: 14px;
width: 100%;
height: 100%;
padding: 10px;
opacity: 0.3;
}
.color-box.is-dark:after {
color: white;
}
@keyframes float-up {
from {
transform: translateY(0);
opacity: 1;
}
to {
transform: translateY(-20px);
opacity: 0;
}
}
.toast {
background-color: black;
position: absolute;
color: white;
border-radius: 8px;
padding: 8px;
animation: float-up 800ms;
animation-timing-function: ease-out;
z-index: 2;
}
</style>
</head>
<body>
${html_colors}
</body>
<script>
document.addEventListener("click", (event) => {
navigator.clipboard.writeText(
\`var(--color-\${event.target.getAttribute(
"data-color-group"
)}-\${event.target.getAttribute(
"data-color-name"
)})\`
);
const toast = document.createElement("div");
document.body.appendChild(toast);
toast.classList.add("toast");
toast.textContent = "Copied!";
toast.style.setProperty(
"top",
event.clientY +
document.scrollingElement.scrollTop -
40 +
"px"
);
toast.style.setProperty(
"left",
event.clientX + "px"
);
toast.addEventListener("animationend", () => {
toast.remove();
});
});
</script>
</html>`,
"html"
);
await Promise.all([
fs.writeFile(resolve(this.project_dir, COLORS_CSS_PATH), css),
fs.writeFile(resolve(this.project_dir, COLORS_HTML_PATH), html),
]);
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 28, 02:13 (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1230023
Default Alt Text
(8 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline