Skip to content

Commit 32de5ed

Browse files
Long Tran (U811370)BOCOVO
authored andcommitted
feat: add the table's color according to table settings
1 parent e88517d commit 32de5ed

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

packages/dbml-to-json-table-schema/src/tests/data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const dbmlTestCode = `
66
inactive [note: "a note"]
77
}
88
9-
Table follows {
9+
Table follows [headercolor: #3498DB] {
1010
id integer [primary key, not null, increment, note: "a note"]
1111
view integer [default: 0, note: "a note"]
1212
following_user_id integer [note: "a note"]
@@ -78,6 +78,7 @@ export const dbmlTestCodeInJSONTableFormat = {
7878
{
7979
name: "follows",
8080
note: "a note",
81+
headerColor: "#3498DB",
8182
fields: [
8283
{
8384
name: "id",

packages/dbml-to-json-table-schema/src/utils/transfomers/dbmlTableToJSONTableTable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import { dbmlIndexToJSONTableIndex } from "./dbmlIndexToJSONTableIndex";
66
import type Table from "@dbml/core/types/model_structure/table";
77

88
export const dbmlTableToJSONTableTable = (
9-
{ name, note, fields, indexes }: Table,
9+
{ name, note, headerColor, fields, indexes }: Table,
1010
relationalFieldMap: Map<string, string[]>,
1111
enumsMap: Set<string>,
1212
): JSONTableTable => {
1313
return {
1414
name,
15+
headerColor,
1516
// the note returned by the dbml parser is not string
1617
// but an object there is an typing error in their package
1718
note: (note as any)?.value,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function getContrastColor(hexColor: string) {
2+
// Convert hex color to RGB values
3+
const r = parseInt(hexColor.slice(1, 3), 16);
4+
const g = parseInt(hexColor.slice(3, 5), 16);
5+
const b = parseInt(hexColor.slice(5, 7), 16);
6+
7+
// Lighten the RGB values by the specified amount (0-100)
8+
const lightenAmount = 0.95;
9+
const newR = Math.round(r + (255 - r) * lightenAmount);
10+
const newG = Math.round(g + (255 - g) * lightenAmount);
11+
const newB = Math.round(b + (255 - b) * lightenAmount);
12+
13+
// Convert the new RGB values back to a hexadecimal color code
14+
const newHexColor = '#' + ((1 << 24) + (newR << 16) + (newG << 8) + newB).toString(16).slice(1);
15+
16+
return newHexColor;
17+
}

packages/json-table-schema-visualizer/src/utils/createTablesColorMap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type JSONTableTable } from "shared/types/tableSchema";
22

33
import { getTableColorFromName } from "./colors/getTableColorFromName";
4+
import { getContrastColor } from "./colors/getContrastColor";
45

56
import { type TableColors } from "@/types/tableColor";
67

@@ -9,7 +10,7 @@ export const createTablesColorMap = (
910
): Map<string, TableColors> => {
1011
const tableColors = new Map<string, TableColors>();
1112
tables.forEach((table) => {
12-
const tableColor = getTableColorFromName(table.name);
13+
const tableColor = !!table.headerColor ? { regular: table.headerColor, lighter: getContrastColor(table.headerColor) } : getTableColorFromName(table.name);
1314

1415
tableColors.set(table.name, tableColor);
1516
});

packages/shared/types/tableSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface JSONTableIndex
4646
}
4747

4848
export interface JSONTableTable
49-
extends PartialRequired<Pick<Table, "name" | "note">, "name"> {
49+
extends PartialRequired<Pick<Table, "name" | "note" | "headerColor">, "name"> {
5050
fields: JSONTableField[];
5151
indexes: JSONTableIndex[];
5252
}

0 commit comments

Comments
 (0)