Skip to content

Instantly share code, notes, and snippets.

@nonaybay
Created September 12, 2021 22:19
Show Gist options
  • Save nonaybay/ab1ac58aa260235a12538ecdbe289c86 to your computer and use it in GitHub Desktop.
Save nonaybay/ab1ac58aa260235a12538ecdbe289c86 to your computer and use it in GitHub Desktop.
@use 'sass:color';
@use 'sass:list';
@use 'sass:map';
@use 'sass:math';
$xterm: (
1: #000000,
2: #800000,
3: #008000,
4: #808000,
5: #000080,
6: #800080,
7: #008080,
8: #c0c0c0,
9: #808080,
10: #ff0000,
11: #00ff00,
12: #ffff00,
13: #0000ff,
14: #ff00ff,
15: #00ffff,
16: #ffffff,
17: #000000,
18: #00005f,
19: #000087,
20: #0000af,
21: #0000d7,
22: #0000ff,
23: #005f00,
24: #005f5f,
25: #005f87,
26: #005faf,
27: #005fd7,
28: #005fff,
29: #008700,
30: #00875f,
31: #008787,
32: #0087af,
33: #0087d7,
34: #0087ff,
35: #00af00,
36: #00af5f,
37: #00af87,
38: #00afaf,
39: #00afd7,
40: #00afff,
41: #00d700,
42: #00d75f,
43: #00d787,
44: #00d7af,
45: #00d7d7,
46: #00d7ff,
47: #00ff00,
48: #00ff5f,
49: #00ff87,
50: #00ffaf,
51: #00ffd7,
52: #00ffff,
53: #5f0000,
54: #5f005f,
55: #5f0087,
56: #5f00af,
57: #5f00d7,
58: #5f00ff,
59: #5f5f00,
60: #5f5f5f,
61: #5f5f87,
62: #5f5faf,
63: #5f5fd7,
64: #5f5fff,
65: #5f8700,
66: #5f875f,
67: #5f8787,
68: #5f87af,
69: #5f87d7,
70: #5f87ff,
71: #5faf00,
72: #5faf5f,
73: #5faf87,
74: #5fafaf,
75: #5fafd7,
76: #5fafff,
77: #5fd700,
78: #5fd75f,
79: #5fd787,
80: #5fd7af,
81: #5fd7d7,
82: #5fd7ff,
83: #5fff00,
84: #5fff5f,
85: #5fff87,
86: #5fffaf,
87: #5fffd7,
88: #5fffff,
89: #870000,
90: #87005f,
91: #870087,
92: #8700af,
93: #8700d7,
94: #8700ff,
95: #875f00,
96: #875f5f,
97: #875f87,
98: #875faf,
99: #875fd7,
100: #875fff,
101: #878700,
102: #87875f,
103: #878787,
104: #8787af,
105: #8787d7,
106: #8787ff,
107: #87af00,
108: #87af5f,
109: #87af87,
110: #87afaf,
111: #87afd7,
112: #87afff,
113: #87d700,
114: #87d75f,
115: #87d787,
116: #87d7af,
117: #87d7d7,
118: #87d7ff,
119: #87ff00,
120: #87ff5f,
121: #87ff87,
122: #87ffaf,
123: #87ffd7,
124: #87ffff,
125: #af0000,
126: #af005f,
127: #af0087,
128: #af00af,
129: #af00d7,
130: #af00ff,
131: #af5f00,
132: #af5f5f,
133: #af5f87,
134: #af5faf,
135: #af5fd7,
136: #af5fff,
137: #af8700,
138: #af875f,
139: #af8787,
140: #af87af,
141: #af87d7,
142: #af87ff,
143: #afaf00,
144: #afaf5f,
145: #afaf87,
146: #afafaf,
147: #afafd7,
148: #afafff,
149: #afd700,
150: #afd75f,
151: #afd787,
152: #afd7af,
153: #afd7af,
154: #afd7ff,
155: #afff00,
156: #afff5f,
157: #afff87,
158: #afffaf,
159: #afffd7,
160: #afffff,
161: #d70000,
162: #d7005f,
163: #d70087,
164: #d700af,
165: #d700d7,
166: #d700ff,
167: #d75f00,
168: #d75f5f,
169: #d75f87,
170: #d75faf,
171: #d75fd7,
172: #d75fff,
173: #d78700,
174: #d7875f,
175: #d78787,
176: #d787af,
177: #d787d7,
178: #d787ff,
179: #d7af00,
180: #d7af5f,
181: #d7af87,
182: #d7afaf,
183: #d7afd7,
184: #d7afff,
185: #d7d700,
186: #d7d75f,
187: #d7d787,
188: #d7d7af,
189: #d7d7d7,
190: #d7d7ff,
191: #d7ff00,
192: #d7ff5f,
193: #d7ff87,
194: #d7ffaf,
195: #d7ffd7,
196: #d7ffff,
197: #ff0000,
198: #ff005f,
199: #ff0087,
200: #ff00af,
201: #ff00d7,
202: #ff00ff,
203: #ff5f00,
204: #ff5f5f,
205: #ff5f87,
206: #ff5faf,
207: #ff5fd7,
208: #ff5fff,
209: #ff8700,
210: #ff875f,
211: #ff8787,
212: #ff87af,
213: #ff87d7,
214: #ff87ff,
215: #ffaf00,
216: #ffaf5f,
217: #ffaf87,
218: #ffafaf,
219: #ffafd7,
220: #ffafff,
221: #ffd700,
222: #ffd75f,
223: #ffd787,
224: #ffd7af,
225: #ffd7d7,
226: #ffd7ff,
227: #ffff00,
228: #ffff5f,
229: #ffff87,
230: #ffffaf,
231: #ffffd7,
232: #ffffff,
233: #080808,
234: #121212,
235: #1c1c1c,
236: #262626,
237: #303030,
238: #3a3a3a,
239: #444444,
240: #4e4e4e,
241: #585858,
242: #626262,
243: #6c6c6c,
244: #767676,
245: #808080,
246: #8a8a8a,
247: #949494,
248: #9e9e9e,
249: #a8a8a8,
250: #b2b2b2,
251: #bcbcbc,
252: #c6c6c6,
253: #d0d0d0,
254: #dadada,
255: #e4e4e4,
256: #eeeeee,
);
@function nearest($needle, $term, $list: $xterm, ) {
$mindistance: null;
$distance: ();
$nr: color.red($needle);
$ng: color.green($needle);
$nb: color.blue($needle);
@each $item in $list {
$max: math.pow(2, $term);
$cond-b: list.index($list, $item) <= $max;
@if $cond-b {
$index: list.nth($item, 1);
$color: list.nth($item, 2);
$color-r: color.red($color);
$color-g: color.green($color);
$color-b: color.blue($color);
$calc-r: math.pow(($nr - $color-r), 2);
$calc-g: math.pow(($ng - $color-g), 2);
$calc-b: math.pow(($nb - $color-b), 2);
$calc: $calc-r + $calc-g + $calc-b;
$distance: list.append($distance, $calc, 'comma');
}
}
$mindistance: math.min($distance...);
@return list.index($distance, $mindistance) - 1;
}
@function colorlin($values) {
$colorlinear: ();
@each $color in $values {
$colorlinear: list.append($colorlinear, $color, 'comma');
}
$r: list.nth($colorlinear, 1) * 255;
$g: list.nth($colorlinear, 2) * 255;
$b: list.nth($colorlinear, 3) * 255;
$a: 1;
@if (list.length($colorlinear) == 4) {
$a: list.nth($colorlinear, 4);
}
$rgb: rgba($r, $g, $b, $a);
@return $rgb;
}
githubcolors {
$x: 8;
/* current: #{math.pow(2, $x)} */
color-----black: nearest(#000000, $x);
color-------red: nearest(#dc3545, $x);
color-----green: nearest(#73ba25, $x);
color----yellow: nearest(#ffc107, $x);
color------blue: nearest(#35b9ab, $x);
color-----dgrey: nearest(#42474c, $x);
color-----lgrey: nearest(#d2d5d8, $x);
color-----white: nearest(#ffffff, $x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment