Skip to content

Commit 91dc737

Browse files
silverwindclaude
andauthored
Replace tinycolor2 with colord (#36673)
[`colord`](https://github.com/omgovich/colord) is significantly smaller than [`tinycolor2`](https://github.com/bgrins/TinyColor) (~4KB vs ~29KB minified) and ships its own TypeScript types, removing the need for `@types/tinycolor2`. Behaviour is exactly the same for our use cases. By using `.alpha(1)` we force the function to always output 6-digit hex format (it would output 8-digit for non-opaque colors). --------- Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 87f7291 commit 91dc737

File tree

4 files changed

+13
-28
lines changed

4 files changed

+13
-28
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"swagger-ui-dist": "5.31.1",
5353
"tailwindcss": "3.4.17",
5454
"throttle-debounce": "5.0.2",
55-
"tinycolor2": "1.6.0",
55+
"colord": "2.9.3",
5656
"tippy.js": "6.3.7",
5757
"toastify-js": "1.12.0",
5858
"tributejs": "5.1.3",
@@ -82,7 +82,6 @@
8282
"@types/sortablejs": "1.15.9",
8383
"@types/swagger-ui-dist": "3.30.6",
8484
"@types/throttle-debounce": "5.0.2",
85-
"@types/tinycolor2": "1.4.6",
8685
"@types/toastify-js": "1.12.4",
8786
"@typescript-eslint/parser": "8.56.0",
8887
"@vitejs/plugin-vue": "6.0.4",

pnpm-lock.yaml

Lines changed: 3 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web_src/js/features/codeeditor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import tinycolor from 'tinycolor2';
1+
import {colord} from 'colord';
22
import {basename, extname, isObject, isDarkTheme} from '../utils.ts';
33
import {onInputDebounce} from '../utils/dom.ts';
44
import type MonacoNamespace from 'monaco-editor';
@@ -94,7 +94,7 @@ function updateTheme(monaco: Monaco): void {
9494
// https://github.com/microsoft/monaco-editor/issues/2427
9595
// also, monaco can only parse 6-digit hex colors, so we convert the colors to that format
9696
const styles = window.getComputedStyle(document.documentElement);
97-
const getColor = (name: string) => tinycolor(styles.getPropertyValue(name).trim()).toString('hex6');
97+
const getColor = (name: string) => colord(styles.getPropertyValue(name).trim()).alpha(1).toHex();
9898

9999
monaco.editor.defineTheme('gitea', {
100100
base: isDarkTheme() ? 'vs-dark' : 'vs',

web_src/js/utils/color.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import tinycolor from 'tinycolor2';
2-
import type {ColorInput} from 'tinycolor2';
1+
import {colord} from 'colord';
2+
import type {AnyColor} from 'colord';
33

44
/** Returns relative luminance for a SRGB color - https://en.wikipedia.org/wiki/Relative_luminance */
55
// Keep this in sync with modules/util/color.go
6-
function getRelativeLuminance(color: ColorInput): number {
7-
const {r, g, b} = tinycolor(color).toRgb();
6+
function getRelativeLuminance(color: AnyColor): number {
7+
const {r, g, b} = colord(color).toRgb();
88
return (0.2126729 * r + 0.7151522 * g + 0.072175 * b) / 255;
99
}
1010

11-
function useLightText(backgroundColor: ColorInput): boolean {
11+
function useLightText(backgroundColor: AnyColor): boolean {
1212
return getRelativeLuminance(backgroundColor) < 0.453;
1313
}
1414

15-
/** Given a background color, returns a black or white foreground color that the highest
16-
* contrast ratio. */
15+
/** Given a background color, returns a black or white foreground color with the highest contrast ratio. */
1716
// In the future, the APCA contrast function, or CSS `contrast-color` will be better.
1817
// https://github.com/color-js/color.js/blob/eb7b53f7a13bb716ec8b28c7a56f052cd599acd9/src/contrast/APCA.js#L42
19-
export function contrastColor(backgroundColor: ColorInput): string {
18+
export function contrastColor(backgroundColor: AnyColor): string {
2019
return useLightText(backgroundColor) ? '#fff' : '#000';
2120
}
2221

0 commit comments

Comments
 (0)