Skip to content

Commit

Permalink
fix: changed naming & adjusted api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
noahonyejese committed Jan 30, 2025
1 parent 66f8520 commit ae08a38
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
6 changes: 4 additions & 2 deletions app/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@ export const convertPaletteTypeToDBType = (
case "categorical":
return "CATEGORICAL";
default:
return "CATEGORICAL";
const _exhaustiveCheck: never = type;
return _exhaustiveCheck;
}
};

Expand All @@ -568,7 +569,8 @@ export const convertDBTypeToPaletteType = (
case "CATEGORICAL":
return "categorical";
default:
return "categorical";
const _exhaustiveCheck: never = type;
return _exhaustiveCheck;
}
};

Expand Down
4 changes: 2 additions & 2 deletions app/db/palettes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const createPaletteForUser = async (
};

export const getPalettesForUser = async (
user_id: number
user_id?: number
): Promise<CustomPaletteType[]> => {
const palettes = await prisma.palette.findMany({
where: {
Expand All @@ -49,7 +49,7 @@ export const getPalettesForUser = async (
};

export const deletePaletteForUser = async (paletteId: string) => {
return await prisma.palette.delete({
await prisma.palette.delete({
where: {
paletteId,
},
Expand Down
77 changes: 48 additions & 29 deletions app/palettes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
schemeSet3,
schemeTableau10,
} from "d3-scale-chromatic";
import { nanoid } from "nanoid";

import { hasDimensionColors } from "./charts/shared/colors";
import {
Expand All @@ -37,6 +36,7 @@ import {
SequentialPaletteType,
} from "./config-types";
import { Component } from "./domain/data";
import { createColorId } from "./utils/color-palette-utils";

// Colors
export const getDefaultCategoricalPaletteId = (
Expand Down Expand Up @@ -149,7 +149,7 @@ export const categoricalPalettes: Array<CategoricalPalette> = [

export const DEFAULT_CATEGORICAL_PALETTE_ID = categoricalPalettes[0].value;

type Palette<T> = {
export type Palette<T> = {
label: string;
value: T;
interpolator: (t: number) => string;
Expand Down Expand Up @@ -232,43 +232,57 @@ const MIN_SATURATION = 0.15;
const MAX_LIGHTNESS = 0.95;

type InterpolationOptions = {
midColor?: string;
midColorHex?: string;
};

type InterpolatorResult = {
interpolator: (t: number) => string;
startingColor?: string;
startingColorHex?: string;
};

export const createSequentialInterpolator = (
endColor: string,
startColor?: string
): InterpolatorResult => {
const endHsl = hsl(endColor);
const startHsl = startColor
? hsl(startColor)
type SequentialInterpolatorProps = {
endColorHex: string;
startColorHex?: string;
};

type DivergingInterpolatorProps = {
endColorHex: string;
startColorHex: string;
options?: InterpolationOptions;
};

export const createSequentialInterpolator = ({
endColorHex,
startColorHex,
}: SequentialInterpolatorProps): InterpolatorResult => {
const endHsl = hsl(endColorHex);
const startHsl = startColorHex
? hsl(startColorHex)
: hsl(
endHsl.h,
Math.max(MIN_SATURATION, endHsl.s * 0.3),
Math.min(MAX_LIGHTNESS, endHsl.l + LIGHTNESS_INCREASE)
);

const startColorRgb = startHsl.toString();
const startingColorHex = rgbToHex(startColorRgb);

return {
interpolator: interpolateRgb(startHsl.toString(), endColor),
startingColor: rgbToHex(startHsl.toString()),
interpolator: interpolateRgb(startingColorHex, endColorHex),
startingColorHex,
};
};

export const createDivergingInterpolator = (
startColor: string,
endColor: string,
options: InterpolationOptions = {}
): InterpolatorResult => {
const { midColor } = options;
export const createDivergingInterpolator = ({
startColorHex,
endColorHex,
options = {},
}: DivergingInterpolatorProps): InterpolatorResult => {
const { midColorHex } = options;

if (midColor) {
const leftInterpolator = interpolateRgb(startColor, midColor);
const rightInterpolator = interpolateRgb(midColor, endColor);
if (midColorHex) {
const leftInterpolator = interpolateRgb(startColorHex, midColorHex);
const rightInterpolator = interpolateRgb(midColorHex, endColorHex);

return {
interpolator: (t: number): string => {
Expand All @@ -281,7 +295,7 @@ export const createDivergingInterpolator = (
}

return {
interpolator: interpolateRgb(startColor, endColor),
interpolator: interpolateRgb(startColorHex, endColorHex),
};
};

Expand All @@ -296,24 +310,29 @@ export type ColorsByType = {
diverging: ColorItem[];
};

export const defaultColorValues = (
export const getDefaultColorValues = (
type: CustomPaletteType["type"],
colors: string[]
): ColorItem[] => {
const colorExist = colors.length > 0;

switch (type) {
case "sequential":
return [{ color: colorExist ? colors[0] : "#000000", id: nanoid(4) }];
return [
{ color: colorExist ? colors[0] : "#000000", id: createColorId() },
];
case "diverging":
return [
{ color: colorExist ? colors[0] : "#000000", id: nanoid(4) },
{ color: colorExist ? colors[1] : "#cccccc", id: nanoid(4) },
{ color: colorExist ? colors[2] : "#777777", id: nanoid(4) },
{ color: colorExist ? colors[0] : "#000000", id: createColorId() },
{ color: colorExist ? colors[1] : "#cccccc", id: createColorId() },
{ color: colorExist ? colors[2] : "#777777", id: createColorId() },
];
case "categorical":
return colorExist
? colors.map((color) => ({ color, id: nanoid(4) }))
? colors.map((color) => ({ color, id: createColorId() }))
: [];
default:
const _exhaustiveCheck: never = type;
return _exhaustiveCheck;
}
};
2 changes: 1 addition & 1 deletion app/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ enum PALETTE_TYPE {
model Palette {
paletteId String @id @default(uuid())
name String @unique @db.VarChar(100)
type PALETTE_TYPE @default(CATEGORICAL)
type PALETTE_TYPE
colors String[]
created_at DateTime @default(now()) @db.Timestamp(6)
updated_at DateTime @default(now()) @db.Timestamp(6)
Expand Down
4 changes: 0 additions & 4 deletions app/server/user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ const UserController = controller({
const session = await getServerSession(req, res, nextAuthOptions);
const userId = session?.user?.id;

if (!userId) {
return [];
}

return await getPalettesForUser(userId);
},
deletePalette: async ({ req }) => {
Expand Down

0 comments on commit ae08a38

Please sign in to comment.