diff --git a/index.ts b/index.ts index 57ddb31..37b8283 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,7 @@ import { ServerRequest } from "./deps.ts"; import { GithubAPIClient } from "./src/github_api_client.ts"; import { Card } from "./src/card.ts"; -import { parseParams, CONSTANTS } from "./src/utils.ts"; +import { parseParams, COLORS, CONSTANTS } from "./src/utils.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts"; const client = new GithubAPIClient(); @@ -11,6 +11,15 @@ export default async (req: ServerRequest) => { const username = params.get("username"); const row = params.getNumberValue("row", CONSTANTS.DEFAULT_MAX_ROW); const column = params.getNumberValue("column", CONSTANTS.DEFAULT_MAX_COLUMN); + const themeParam: string = params.getStringValue("theme", "default"); + let theme; + switch (themeParam) { + case "gruvbox": + theme = COLORS.gruvbox; + break; + default: + theme = COLORS.default; + } const paddingWidth = params.getNumberValue( "padding-w", CONSTANTS.DEFAULT_PADDING_W, @@ -58,7 +67,7 @@ export default async (req: ServerRequest) => { CONSTANTS.DEFAULT_PANEL_SIZE, paddingWidth, paddingHeight, - ).render(userInfo), + ).render(userInfo, theme), headers: new Headers( { "Content-Type": "image/svg+xml", diff --git a/src/card.ts b/src/card.ts index 5ee84fe..345a3a7 100644 --- a/src/card.ts +++ b/src/card.ts @@ -11,7 +11,7 @@ import { AncientAccountTrophy, } from "./trophies.ts"; import { UserInfo } from "./github_api_client.ts"; -import { CONSTANTS, RANK_ORDER, RANK } from "./utils.ts"; +import { CONSTANTS, RANK_ORDER, RANK, Theme } from "./utils.ts"; export class Card { private width = 0; @@ -27,7 +27,10 @@ export class Card { ) { this.width = panelSize * this.maxColumn; } - render(userInfo: UserInfo): string { + render( + userInfo: UserInfo, + theme: Theme, + ): string { let trophyList = new Array( new TotalStarTrophy(userInfo.totalStargazers), new TotalCommitTrophy(userInfo.totalCommits), @@ -76,7 +79,7 @@ export class Card { (sum: string, trophy: Trophy, i: number) => { const x = this.panelSize * (i % this.maxColumn); const y = this.panelSize * Math.floor(i / this.maxColumn); - return sum + trophy.render(x, y, this.panelSize); + return sum + trophy.render(theme, x, y, this.panelSize); }, "", ); diff --git a/src/icons.ts b/src/icons.ts index 72bfae8..215c87d 100644 --- a/src/icons.ts +++ b/src/icons.ts @@ -1,11 +1,11 @@ -import { RANK, COLORS } from "./utils.ts"; +import { RANK, Theme } from "./utils.ts"; -const leafIcon = - ` +const leafIcon = (laurel: string): string => { + return ` Created by potrace 1.15, written by Peter Selinger 2001-2017 - + @@ -23,10 +23,12 @@ Created by potrace 1.15, written by Peter Selinger 2001-2017 `; +}; export const getNextRankBar = ( title: string, percentage: number, + color: string, ): string => { const maxWidht = 80; return ` @@ -50,7 +52,7 @@ export const getNextRankBar = ( width="${maxWidht}" height="3.2" opacity="0.3" - fill="${COLORS.NEXT_RANK_BAR}" + fill="${color}" /> `; }; @@ -86,47 +88,48 @@ const getSmallTrophyIcon = ( // Single Rank return ""; }; -export const getTrophyIcon = (rank = RANK.UNKNOWN) => { - let color = COLORS.DEFAULT_RANK_BASE; - let rankColor = COLORS.DEFAULT_RANK_TEXT; +export const getTrophyIcon = (theme: Theme, rank = RANK.UNKNOWN) => { + let color = theme.DEFAULT_RANK_BASE; + let rankColor = theme.DEFAULT_RANK_TEXT; let backgroundIcon = ""; let gradationColor = ` - - - + + + `; + const { ICON_CIRCLE } = theme; if (rank === RANK.SECRET) { - rankColor = COLORS.SECRET_RANK_TEXT; + rankColor = theme.SECRET_RANK_TEXT; gradationColor = ` - - - + + + `; } else if (rank.slice(0, 1) === RANK.S) { - color = COLORS.S_RANK_BASE; - rankColor = COLORS.S_RANK_TEXT; - backgroundIcon = leafIcon; + color = theme.S_RANK_BASE; + rankColor = theme.S_RANK_TEXT; + backgroundIcon = leafIcon(theme.LAUREL); gradationColor = ` - + `; } else if (rank.slice(0, 1) === RANK.A) { - color = COLORS.A_RANK_BASE; - rankColor = COLORS.A_RAKN_TEXT; - backgroundIcon = leafIcon; + color = theme.A_RANK_BASE; + rankColor = theme.A_RAKN_TEXT; + backgroundIcon = leafIcon(theme.LAUREL); gradationColor = ` - + `; } else if (rank === RANK.B) { - color = COLORS.B_RANK_BASE; - rankColor = COLORS.B_RANK_TEXT; + color = theme.B_RANK_BASE; + rankColor = theme.B_RANK_TEXT; gradationColor = ` - + `; } const icon = ` @@ -134,7 +137,7 @@ export const getTrophyIcon = (rank = RANK.UNKNOWN) => { - + ${ rank.slice(0, 1) } diff --git a/src/trophies.ts b/src/trophies.ts index 446544c..e19c257 100644 --- a/src/trophies.ts +++ b/src/trophies.ts @@ -1,5 +1,5 @@ import { getTrophyIcon, getNextRankBar } from "./icons.ts"; -import { CONSTANTS, RANK, abridgeScore, RANK_ORDER, COLORS } from "./utils.ts"; +import { CONSTANTS, RANK, abridgeScore, RANK_ORDER, Theme } from "./utils.ts"; class RankCondition { constructor( @@ -58,10 +58,16 @@ export class Trophy { const result = progress / distance; return result; } - render(x = 0, y = 0, panelSize = CONSTANTS.DEFAULT_PANEL_SIZE): string { + render(theme: Theme, + x = 0, + y = 0, + panelSize = CONSTANTS.DEFAULT_PANEL_SIZE, + ): string { + const { PRIMARY, SECONDARY, TEXT, NEXT_RANK_BAR } = theme; const nextRankBar = getNextRankBar( this.title, this.calculateNextRankPercentage(), + NEXT_RANK_BAR, ); return ` - ${getTrophyIcon(this.rank)} - ${this.title} - ${this.topMessage} - ${this.bottomMessage} + ${getTrophyIcon(theme, this.rank)} + ${this.title} + ${this.topMessage} + ${this.bottomMessage} ${nextRankBar} `; diff --git a/src/utils.ts b/src/utils.ts index 7d00be8..249b0d5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -68,23 +68,79 @@ export enum RANK { } export const COLORS = { - SECRET_RANK_1: "red", - SECRET_RANK_2: "fuchsia", - SECRET_RANK_3: "blue", - SECRET_RANK_TEXT: "fuchsia", - S_RANK_BASE: "#FAD200", - S_RANK_SHADOW: "#C8A090", - S_RANK_TEXT: "#886000", - A_RANK_BASE: "#B0B0B0", - A_RANK_SHADOW: "#9090C0", - A_RAKN_TEXT: "#505050", - B_RANK_BASE: "#A18D66", - B_RANK_SHADOW: "#816D96", - B_RANK_TEXT: "#412D06", - DEFAULT_RANK_BASE: "#777", - DEFAULT_RANK_SHADOW: "#333", - DEFAULT_RANK_TEXT: "#333", - NEXT_RANK_BAR: "#0366d6", + default: { + PRIMARY: "#FFF", + SECONDARY: "#000", + ICON_CIRCLE: "#FFF", + TEXT: "#666", + LAUREL: "#009366", + SECRET_RANK_1: "red", + SECRET_RANK_2: "fuchsia", + SECRET_RANK_3: "blue", + SECRET_RANK_TEXT: "fuchsia", + NEXT_RANK_BAR: "#0366d6", + S_RANK_BASE: "#FAD200", + S_RANK_SHADOW: "#C8A090", + S_RANK_TEXT: "#886000", + A_RANK_BASE: "#B0B0B0", + A_RANK_SHADOW: "#9090C0", + A_RAKN_TEXT: "#505050", + B_RANK_BASE: "#A18D66", + B_RANK_SHADOW: "#816D96", + B_RANK_TEXT: "#412D06", + DEFAULT_RANK_BASE: "#777", + DEFAULT_RANK_SHADOW: "#333", + DEFAULT_RANK_TEXT: "#333", + }, + gruvbox: { + PRIMARY: "#282828", + SECONDARY: "#ebdbb2", + ICON_CIRCLE: "#ebdbb2", + TEXT: "#98971a", + LAUREL: "#8ec07c", + SECRET_RANK_1: "#fb4934", + SECRET_RANK_2: "#d3869b", + SECRET_RANK_3: "#458588", + SECRET_RANK_TEXT: "#d3869b", + NEXT_RANK_BAR: "#d79921", + S_RANK_BASE: "#fabd2f", + S_RANK_SHADOW: "#fabd2f", + S_RANK_TEXT: "#322301", + A_RANK_BASE: "#83a598", + A_RANK_SHADOW: "#83a598", + A_RAKN_TEXT: "#151e1a", + B_RANK_BASE: "#d65d0e", + B_RANK_SHADOW: "#d65d0e", + B_RANK_TEXT: "#301503", + DEFAULT_RANK_BASE: "#928374", + DEFAULT_RANK_SHADOW: "#928374", + DEFAULT_RANK_TEXT: "#282828", + } +}; + +export interface Theme { + PRIMARY: string; + SECONDARY: string; + ICON_CIRCLE: string; + TEXT: string; + LAUREL: string; + SECRET_RANK_1: string; + SECRET_RANK_2: string; + SECRET_RANK_3: string; + SECRET_RANK_TEXT: string; + NEXT_RANK_BAR: string; + S_RANK_BASE: string; + S_RANK_SHADOW: string; + S_RANK_TEXT: string; + A_RANK_BASE: string; + A_RANK_SHADOW: string; + A_RAKN_TEXT: string; + B_RANK_BASE: string; + B_RANK_SHADOW: string; + B_RANK_TEXT: string; + DEFAULT_RANK_BASE: string; + DEFAULT_RANK_SHADOW: string; + DEFAULT_RANK_TEXT: string; }; export const RANK_ORDER = Object.values(RANK);