-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from fosslife/master
- Loading branch information
Showing
14 changed files
with
446 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
ColorPicker, | ||
ColorSwatch, | ||
Group, | ||
isLightColor, | ||
Stack, | ||
Text, | ||
} from "@mantine/core"; | ||
import { generateColorsMap } from "./generator"; | ||
import { useState } from "react"; | ||
import classes from "./colorgenerator.module.css"; | ||
|
||
export default function ColorGenerator() { | ||
const [selectedColor, setSelectedColor] = useState("#FF007F"); | ||
|
||
const { baseColorIndex, colors } = generateColorsMap(selectedColor); | ||
return ( | ||
<Stack> | ||
<ColorPicker | ||
w="100%" | ||
value={selectedColor} | ||
style={{ border: "1px solid #ccc" }} | ||
onChange={(e) => setSelectedColor(e)} | ||
/> | ||
<Text | ||
w="fit-content" | ||
p="5px" | ||
c={selectedColor} | ||
bg={isLightColor(selectedColor) ? "dark" : "white"} | ||
> | ||
{" "} | ||
Selected color: {selectedColor} | ||
</Text> | ||
<Group grow gap={0}> | ||
{colors?.map((color, index) => ( | ||
<div key={index} className={classes.item}> | ||
<ColorSwatch | ||
color={color.hex()} | ||
radius={0} | ||
className={classes.swatch} | ||
withShadow={false} | ||
data-base={index === baseColorIndex || undefined} | ||
c={isLightColor(color.hex()) ? "black" : "white"} | ||
> | ||
{ | ||
<div className={classes.label}> | ||
<span className={classes.index}>{index}</span> | ||
<span className={classes.hex}>{color.hex()}</span> | ||
</div> | ||
} | ||
</ColorSwatch> | ||
</div> | ||
))} | ||
</Group> | ||
<Text c="dimmed"> | ||
Inspired from: https://mantine.dev/colors-generator | ||
</Text> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
item { | ||
flex: 1; | ||
|
||
&:first-of-type > .swatch { | ||
border-top-left-radius: var(--mantine-radius-md) !important; | ||
border-bottom-left-radius: var(--mantine-radius-md) !important; | ||
} | ||
|
||
&:last-of-type > .swatch { | ||
border-top-right-radius: var(--mantine-radius-md) !important; | ||
border-bottom-right-radius: var(--mantine-radius-md) !important; | ||
} | ||
} | ||
|
||
.swatch { | ||
width: 100%; | ||
height: 0; | ||
padding-bottom: 100%; | ||
overflow: hidden; | ||
transition: | ||
transform 100ms ease, | ||
box-shadow 100ms ease, | ||
border-radius 100ms ease; | ||
|
||
&[data-base] { | ||
transform: scale(1.2) translateY(-5px); | ||
z-index: 1; | ||
box-shadow: var(--mantine-shadow-md); | ||
border-radius: var(--mantine-radius-md); | ||
|
||
@media (max-width: em(600px)) { | ||
transform: none; | ||
box-shadow: none; | ||
border-radius: 0; | ||
} | ||
} | ||
} | ||
|
||
.label { | ||
display: flex; | ||
flex-direction: column; | ||
text-align: center; | ||
gap: 5px; | ||
|
||
@media (max-width: em(600px)) { | ||
display: none; | ||
} | ||
} | ||
|
||
.hex { | ||
font-weight: 700; | ||
text-transform: uppercase; | ||
font-family: var(--mantine-font-family-monospace); | ||
font-size: 10px; | ||
} | ||
|
||
.index { | ||
font-size: var(--mantine-font-size-lg); | ||
font-weight: 700; | ||
font-family: var(--docs-font-primary); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import chroma from "chroma-js"; | ||
|
||
const LIGHTNESS_MAP = [ | ||
0.96, 0.907, 0.805, 0.697, 0.605, 0.547, 0.518, 0.445, 0.395, 0.34, | ||
]; | ||
const SATURATION_MAP = [0.32, 0.16, 0.08, 0.04, 0, 0, 0.04, 0.08, 0.16, 0.32]; | ||
|
||
function getClosestLightness(colorObject: chroma.Color) { | ||
const lightnessGoal = colorObject.get("hsl.l"); | ||
return LIGHTNESS_MAP.reduce((prev, curr) => | ||
Math.abs(curr - lightnessGoal) < Math.abs(prev - lightnessGoal) | ||
? curr | ||
: prev | ||
); | ||
} | ||
|
||
export function generateColorsMap(color: string) { | ||
const colorObject = chroma(color); | ||
const closestLightness = getClosestLightness(colorObject); | ||
const baseColorIndex = LIGHTNESS_MAP.findIndex((l) => l === closestLightness); | ||
|
||
const colors = LIGHTNESS_MAP.map((l) => colorObject.set("hsl.l", l)) | ||
.map((c) => chroma(c)) | ||
.map((c, i) => { | ||
const saturationDelta = | ||
SATURATION_MAP[i] - SATURATION_MAP[baseColorIndex]; | ||
return saturationDelta >= 0 | ||
? c.saturate(saturationDelta) | ||
: c.desaturate(saturationDelta * -1); | ||
}); | ||
|
||
return { baseColorIndex, colors }; | ||
} | ||
|
||
export type MantineColorsTuple = readonly [ | ||
string, | ||
string, | ||
string, | ||
string, | ||
string, | ||
string, | ||
string, | ||
string, | ||
string, | ||
string, | ||
...string[], | ||
]; | ||
|
||
export function generateColors(color: string) { | ||
return generateColorsMap(color).colors.map((c) => | ||
c.hex() | ||
) as unknown as MantineColorsTuple; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Divider, Stack, Textarea, TextInput } from "@mantine/core"; | ||
import { useInputState } from "@mantine/hooks"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function HmacGenerator() { | ||
const [hmac, setHmac] = useState(""); | ||
const [input, setInput] = useInputState(""); | ||
const [secret, setSecret] = useInputState(""); | ||
|
||
useEffect(() => { | ||
if (input && secret) { | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(input); | ||
const key = encoder.encode(secret); | ||
crypto.subtle | ||
.importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, [ | ||
"sign", | ||
]) | ||
.then((key) => | ||
crypto.subtle.sign("HMAC", key, data).then((signature) => { | ||
const view = new DataView(signature); | ||
let hex = ""; | ||
for (let i = 0; i < view.byteLength; i += 1) { | ||
const byte = view.getUint8(i).toString(16); | ||
hex += byte.length === 1 ? `0${byte}` : byte; | ||
} | ||
setHmac(hex); | ||
}) | ||
); | ||
} else { | ||
setHmac(""); | ||
} | ||
}, [input, secret]); | ||
|
||
return ( | ||
<Stack> | ||
<Textarea | ||
minRows={5} | ||
autosize | ||
value={input} | ||
onChange={setInput} | ||
placeholder="Enter input data" | ||
/> | ||
<TextInput | ||
placeholder="Enter secret key" | ||
value={secret} | ||
onChange={setSecret} | ||
/> | ||
<Divider label="HMAC" /> | ||
<Textarea placeholder="generated HMAC" value={hmac} readOnly /> | ||
</Stack> | ||
); | ||
} |
Oops, something went wrong.