Skip to content

Commit

Permalink
added manual color generation code
Browse files Browse the repository at this point in the history
  • Loading branch information
timephy committed May 27, 2024
1 parent 54e210c commit d742af3
Show file tree
Hide file tree
Showing 12 changed files with 653 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/tui-colors-tw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"lint": "prettier --check . && eslint . && publint",
"check:publish": "pnpm run lint && "
},
"files": [
"index.js",
"colors.js"
],
"author": "",
"license": "MIT",
"peerDependencies": {
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions packages/tui-colors-tw/tui-color-gen/colors/base_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from color import TUI_Color

colors = [
TUI_Color(
'step-base',
(248, 248, 248),
(15, 15, 15)
)
]
8 changes: 8 additions & 0 deletions packages/tui-colors-tw/tui-color-gen/colors/color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class TUI_Color:
name: str
light: tuple[int, int, int]
dark: tuple[int, int, int]
100 changes: 100 additions & 0 deletions packages/tui-colors-tw/tui-color-gen/colors/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from generate_colors import colors
from generate_step_colors_two_parts import colors as colors_steps
from base_colors import colors as colors_base

colors = [*colors_base, *colors_steps, *colors]

# CSS Colors

colors_rgb_light = [
f"--{color.name}-rgb-light: {color.light[0]}, {color.light[1]}, {color.light[2]};" for color in colors
]
color_rgbs_dark = [
f"--{color.name}-rgb-dark: {color.dark[0]}, {color.dark[1]}, {color.dark[2]};" for color in colors
]

colors_light = [
f"--{color.name}-light: rgb(var(--{color.name}-rgb-light));" for color in colors
]
colors_dark = [
f"--{color.name}-dark: rgb(var(--{color.name}-rgb-dark));" for color in colors
]

colors_rgb_if_light = [
f"--{color.name}-rgb: var(--{color.name}-rgb-light);" for color in colors
]
colors_rgb_if_dark = [
f"--{color.name}-rgb: var(--{color.name}-rgb-dark);" for color in colors
]

colors_def = [
f"--{color.name}: rgb(var(--{color.name}-rgb));" for color in colors
]

# Tailwind Color Config

tailwind_config_colors_light = [
f"\"{color.name}-light\": \"rgb(var(--{color.name}-rgb-light), <alpha-value>)\"," for color in colors
]
tailwind_config_colors_dark = [
f"\"{color.name}-dark\": \"rgb(var(--{color.name}-rgb-dark), <alpha-value>)\"," for color in colors
]
tailwind_config_colors = [
f"\"{color.name}\": \"rgb(var(--{color.name}-rgb), <alpha-value>)\"," for color in colors
]

# Export format

content_colors_rgb_light = "\n".join(colors_rgb_light)
content_color_rgbs_dark = "\n".join(color_rgbs_dark)
content_colors_light = "\n".join(colors_light)
content_colors_dark = "\n".join(colors_dark)
content_colors_rgb_if_light = "\n".join(colors_rgb_if_light)
content_colors_rgb_if_dark = "\n".join(colors_rgb_if_dark)
content_colors_def = "\n".join(colors_def)

content = f"""
html {{
{content_colors_rgb_light}
}}
html {{
{content_color_rgbs_dark}
}}
html {{
{content_colors_light}
}}
html {{
{content_colors_dark}
}}
html {{
{content_colors_rgb_if_light}
}}
html.dark {{
{content_colors_rgb_if_dark}
}}
html {{
{content_colors_def}
}}
"""

content_tailwind_config_colors_light = "\n".join(tailwind_config_colors_light)
content_tailwind_config_colors_dark = "\n".join(tailwind_config_colors_dark)
content_tailwind_config_colors = "\n".join(tailwind_config_colors)

content_tailwind = f"""
{content_tailwind_config_colors_light}
{content_tailwind_config_colors_dark}
{content_tailwind_config_colors}
"""

with open('../export/colors.css', 'w') as f:
f.write(content)

with open('../export/tailwind-colors.txt', 'w') as f:
f.write(content_tailwind)
118 changes: 118 additions & 0 deletions packages/tui-colors-tw/tui-color-gen/colors/generate_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from pprint import pprint
from dataclasses import dataclass
from colour import Color
import numpy as np

from color import TUI_Color


with open("../source-colors-from-apple.txt", "r") as f:
source_colors = f.readlines()
source_colors = [x.strip() for x in source_colors]

LINES_PER_COLOR = 7
count_colors = len(source_colors) // LINES_PER_COLOR


@dataclass
class Local_Color:
name: str

light_000: Color
dark_000: Color

light_050: Color
light_100: Color
light_150: Color
light_200: Color
light_250: Color

dark_050: Color
dark_100: Color
dark_150: Color
dark_200: Color
dark_250: Color

def __init__(self, name: str, light: Color, dark: Color):
self.name = name

dark = Color(dark)
dark.set_luminance(dark.get_luminance() * 0.85)
dark.set_saturation(dark.get_saturation() * 0.8)

self.dark_000 = Color(dark)
self.dark_000.set_luminance(dark.get_luminance() * 1.0)
self.dark_050 = Color(dark)
self.dark_050.set_luminance(dark.get_luminance() * 1.075)
self.dark_100 = Color(dark)
self.dark_100.set_luminance(dark.get_luminance() * 1.1)
self.dark_150 = Color(dark)
self.dark_150.set_luminance(dark.get_luminance() * 1.175)
self.dark_200 = Color(dark)
self.dark_200.set_luminance(dark.get_luminance() * 1.25)
self.dark_250 = Color(dark)
self.dark_250.set_luminance(dark.get_luminance() * 1.325)

light = Color(light)
light.set_luminance(light.get_luminance() * 1.5)
# light.set_saturation(light.get_saturation() * 0.8)

self.light_000 = Color(light)
self.light_000.set_luminance(light.get_luminance() * 1.0)
self.light_050 = Color(light)
self.light_050.set_luminance(light.get_luminance() * 0.925)
self.light_100 = Color(light)
self.light_100.set_luminance(light.get_luminance() * 0.9)
self.light_150 = Color(light)
self.light_150.set_luminance(light.get_luminance() * 0.825)
self.light_200 = Color(light)
self.light_200.set_luminance(light.get_luminance() * 0.75)
self.light_250 = Color(light)
self.light_250.set_luminance(light.get_luminance() * 0.675)


local_colors: list[Local_Color] = []
for i in range(count_colors):
source = source_colors[i*LINES_PER_COLOR:i*LINES_PER_COLOR+LINES_PER_COLOR]
(r_light, g_light, b_light) = [int(x[2:]) for x in source[0:3]]
(r_dark, g_dark, b_dark) = [int(x[2:]) for x in source[3:6]]

name = source[6].split("\t")[0].lower() # "red" | "green" | "blue"
local_colors.append(Local_Color(name, Color(rgb=(r_light/255, g_light/255, b_light/255)),
Color(rgb=(r_dark/255, g_dark/255, b_dark/255))))


colors: list[TUI_Color] = []
for color in local_colors:
colors.append(TUI_Color(
f'{color.name}-000',
tuple(np.rint(np.array(color.light_000.get_rgb()) * 255).astype(int)),
tuple(np.rint(np.array(color.dark_000.get_rgb()) * 255).astype(int)),
))
colors.append(TUI_Color(
f'{color.name}-050',
tuple(np.rint(np.array(color.light_050.get_rgb()) * 255).astype(int)),
tuple(np.rint(np.array(color.dark_050.get_rgb()) * 255).astype(int)),
))
colors.append(TUI_Color(
f'{color.name}-100',
tuple(np.rint(np.array(color.light_100.get_rgb()) * 255).astype(int)),
tuple(np.rint(np.array(color.dark_100.get_rgb()) * 255).astype(int)),
))
colors.append(TUI_Color(
f'{color.name}-150',
tuple(np.rint(np.array(color.light_150.get_rgb()) * 255).astype(int)),
tuple(np.rint(np.array(color.dark_150.get_rgb()) * 255).astype(int)),
))
colors.append(TUI_Color(
f'{color.name}-200',
tuple(np.rint(np.array(color.light_200.get_rgb()) * 255).astype(int)),
tuple(np.rint(np.array(color.dark_200.get_rgb()) * 255).astype(int)),
))
colors.append(TUI_Color(
f'{color.name}-250',
tuple(np.rint(np.array(color.light_250.get_rgb()) * 255).astype(int)),
tuple(np.rint(np.array(color.dark_250.get_rgb()) * 255).astype(int)),
))

pprint(colors)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pprint import pprint
import numpy as np

from color import TUI_Color

N_1 = 5 + 5 # 000-450
N_2 = 5 + 4 # 500-900
N = N_1 + N_2

steps = np.linspace(0, 900, N).astype(int)
print(f'{steps = }')

values_1 = np.linspace(23, 105, N_1)
values_2 = np.linspace(155, 255, N_2)

values = np.rint(np.concatenate((values_1, values_2))).astype(int)
print(f'{values = }')

for val in values:
print(val)

# Export

colors = [
TUI_Color(
f"step-{step:03d}",
(value_light, value_light, value_light),
(value_dark, value_dark, value_dark)
)
for step, value_light, value_dark in zip(steps, values[::-1], values)
]
pprint(colors)
112 changes: 112 additions & 0 deletions packages/tui-colors-tw/tui-color-gen/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from colour import Color
from dataclasses import dataclass

# with open("source-colors-from-apple.txt", "r") as f:
with open("tweaked-colors-by-tim.txt", "r") as f:
source_colors = f.readlines()
source_colors = [x.strip() for x in source_colors]

LINES_PER_COLOR = 7
count_colors = len(source_colors) // LINES_PER_COLOR


@dataclass
class TUI_Color:
name: str

light_000: Color
dark_000: Color

light_050: Color | None = None
light_100: Color | None = None
light_150: Color | None = None
light_200: Color | None = None
light_250: Color | None = None

dark_050: Color | None = None
dark_100: Color | None = None
dark_150: Color | None = None
dark_200: Color | None = None
dark_250: Color | None = None

def __init__(self, name: str, light: Color, dark: Color):
self.name = name

dark = Color(dark)
dark.set_luminance(dark.get_luminance() * 0.85)
dark.set_saturation(dark.get_saturation() * 0.8)

self.dark_000 = Color(dark)
self.dark_000.set_luminance(dark.get_luminance() * 1.0)
self.dark_050 = Color(dark)
self.dark_050.set_luminance(dark.get_luminance() * 1.075)
self.dark_100 = Color(dark)
self.dark_100.set_luminance(dark.get_luminance() * 1.1)
self.dark_150 = Color(dark)
self.dark_150.set_luminance(dark.get_luminance() * 1.175)
self.dark_200 = Color(dark)
self.dark_200.set_luminance(dark.get_luminance() * 1.25)
self.dark_250 = Color(dark)
self.dark_250.set_luminance(dark.get_luminance() * 1.325)

light = Color(light)
light.set_luminance(light.get_luminance() * 1.5)
# light.set_saturation(light.get_saturation() * 0.8)

self.light_000 = Color(light)
self.light_000.set_luminance(light.get_luminance() * 1.0)
self.light_050 = Color(light)
self.light_050.set_luminance(light.get_luminance() * 0.925)
self.light_100 = Color(light)
self.light_100.set_luminance(light.get_luminance() * 0.9)
self.light_150 = Color(light)
self.light_150.set_luminance(light.get_luminance() * 0.825)
self.light_200 = Color(light)
self.light_200.set_luminance(light.get_luminance() * 0.75)
self.light_250 = Color(light)
self.light_250.set_luminance(light.get_luminance() * 0.675)


colors: list[TUI_Color] = []
for i in range(count_colors):
source = source_colors[i*LINES_PER_COLOR:i*LINES_PER_COLOR+LINES_PER_COLOR]
(r_light, g_light, b_light) = [int(x[2:]) for x in source[0:3]]
(r_dark, g_dark, b_dark) = [int(x[2:]) for x in source[3:6]]
name = source[6].split("\t")[0].lower()
colors.append(TUI_Color(name, Color(rgb=(r_light/255, g_light/255, b_light/255)),
Color(rgb=(r_dark/255, g_dark/255, b_dark/255))))


output: list[str] = []
for color in colors:
# color.dark_000.set_luminance(color.dark_000.get_luminance() * 0.8)
# color.dark_000.set_saturation(color.dark_000.get_saturation() * 0.8)
# color.dark_100 = Color(color.dark_000)
# color.dark_100.set_luminance(color.dark_100.get_luminance() * 1.1)
# color.dark_200 = Color(color.dark_000)
# color.dark_200.set_luminance(color.dark_200.get_luminance() * 1.25)

# color.light_000.set_luminance(color.light_000.get_luminance() * 1.4)
# # color.light.set_saturation(color.light.get_saturation() * 0.8)
# color.light_100 = Color(color.light_000)
# color.light_100.set_luminance(color.light_100.get_luminance() * 0.9)
# color.light_200 = Color(color.light_000)
# color.light_200.set_luminance(color.light_200.get_luminance() * 0.75)

output.append(f'"{color.name}-000": "{color.light_000}",')
output.append(f'"{color.name}-050": "{color.light_050}",')
output.append(f'"{color.name}-100": "{color.light_100}",')
output.append(f'"{color.name}-150": "{color.light_150}",')
output.append(f'"{color.name}-200": "{color.light_200}",')
output.append(f'"{color.name}-250": "{color.light_250}",')

output.append(f'"{color.name}-000-dark": "{color.dark_000}",')
output.append(f'"{color.name}-050-dark": "{color.dark_050}",')
output.append(f'"{color.name}-100-dark": "{color.dark_100}",')
output.append(f'"{color.name}-150-dark": "{color.dark_150}",')
output.append(f'"{color.name}-200-dark": "{color.dark_200}",')
output.append(f'"{color.name}-250-dark": "{color.dark_250}",')


for o in output:
print(o)
Loading

0 comments on commit d742af3

Please sign in to comment.