forked from pinterest/querybook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: find the closed color in ColorPalette (pinterest#1184)
* feat: find the closed color in ColorPalette * comments * add back _asdict * move color palette to yaml * jest to load yaml * remove hex color check * move js-yaml to dev dependency
- Loading branch information
1 parent
e9db8f1
commit 9dd64ea
Showing
13 changed files
with
134 additions
and
55 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
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,48 @@ | ||
- name: blue | ||
color: '#35B5BB' | ||
fillColor: rgba(53, 181, 187, 0.25) | ||
- name: pink | ||
color: '#ff3975' | ||
fillColor: rgba(255, 57, 117, 0.25) | ||
- name: grey | ||
color: '#bfbfbf' | ||
fillColor: rgba(191, 191, 191, 0.25) | ||
- name: gold | ||
color: '#ffca00' | ||
fillColor: rgba(255, 202, 0, 0.25) | ||
- name: picton blue | ||
color: '#529dce' | ||
fillColor: rgba(82, 157, 206, 0.25) | ||
- name: orange | ||
color: '#ff9f42' | ||
fillColor: rgba(255, 159, 66, 0.25) | ||
- name: creamy forest green | ||
color: '#6ba097' | ||
fillColor: rgba(107, 160, 151, 0.25) | ||
- name: chartreuse | ||
color: '#aee800' | ||
fillColor: rgba(174, 232, 0, 0.25) | ||
- name: baby pink | ||
color: '#ff91c8' | ||
fillColor: rgba(255, 145, 200, 0.25) | ||
- name: icy blue | ||
color: '#85d0ce' | ||
fillColor: rgba(133, 208, 206, 0.25) | ||
- name: fuscia | ||
color: '#eb37ce' | ||
fillColor: rgba(235, 55, 206, 0.25) | ||
- name: light purple | ||
color: '#C792EA' | ||
fillColor: rgba(199, 146, 234, 0.25) | ||
- name: salmon | ||
color: '#ec7f77' | ||
fillColor: rgba(236, 127, 119, 0.25) | ||
- name: olive | ||
color: '#989801' | ||
fillColor: rgba(152, 152, 1, 0.25) | ||
- name: beige | ||
color: '#ecd1af' | ||
fillColor: rgba(236, 209, 175, 0.25) | ||
- name: choco | ||
color: '#b7652b' | ||
fillColor: rgba(183, 101, 43, 0.25) |
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,8 @@ | ||
from typing import TypedDict | ||
|
||
|
||
class PaletteColor(TypedDict): | ||
name: str | ||
# color in hex format, e.g. #4287f5 | ||
color: str | ||
fillColor: str |
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,44 @@ | ||
import math | ||
|
||
from const.color import PaletteColor | ||
from lib.config import get_config_value | ||
|
||
|
||
color_palette = get_config_value("color_palette") | ||
|
||
|
||
def convert_hex_to_rgb(hex_color: str) -> tuple[int, int, int]: | ||
r = int(hex_color[1:3], 16) | ||
g = int(hex_color[3:5], 16) | ||
b = int(hex_color[5:7], 16) | ||
return (r, g, b) | ||
|
||
|
||
def find_nearest_palette_color(hex_color: str) -> PaletteColor: | ||
"""Given a hex color, find the nearest color from the color palette.""" | ||
# Return the given color if it's in the color palette | ||
exact_color = next( | ||
(color for color in color_palette if color["color"] == "#529dce"), None | ||
) | ||
if exact_color: | ||
return exact_color | ||
|
||
# Convert the hex color to RGB | ||
given_rgb_color = convert_hex_to_rgb(hex_color) | ||
|
||
# Calculate the Euclidean distance between the given color and each color in the palette | ||
min_distance = math.inf | ||
nearest_color = None | ||
for color in color_palette: | ||
platte_hex_color = color["color"] | ||
palette_rgb_color = convert_hex_to_rgb(platte_hex_color) | ||
distance = ( | ||
(given_rgb_color[0] - palette_rgb_color[0]) ** 2 | ||
+ (given_rgb_color[1] - palette_rgb_color[1]) ** 2 | ||
+ (given_rgb_color[2] - palette_rgb_color[2]) ** 2 | ||
) | ||
if distance < min_distance: | ||
min_distance = distance | ||
nearest_color = color | ||
|
||
return nearest_color |
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
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,11 @@ | ||
const yaml = require('js-yaml'); | ||
|
||
module.exports = { | ||
process: (src, filename) => { | ||
if (filename.endsWith('.yaml') || filename.endsWith('.yml')) { | ||
const data = yaml.load(src); | ||
return `module.exports = ${JSON.stringify(data)};`; | ||
} | ||
return src; | ||
}, | ||
}; |