Skip to content

Instantly share code, notes, and snippets.

@ameliabradley
ameliabradley / constparse.rs
Last active July 20, 2020 17:48
Parse string to number (usize) in a constant function in Rust
// Based on Gist by DutchGhost
// https://gist.github.com/DutchGhost/d8604a3c796479777fe9f5e25d855cfd
// Changes:
// - Conditional compilation to support either 64 or 32-bit
// - Added panic error message
// - Added convenience function parse_unwrap
// - Added tests
#![feature(const_if_match)]
#![feature(const_panic)]
@ameliabradley
ameliabradley / getcolor.js
Last active January 5, 2017 21:20
Given a hex color, finds the optimal font color (black or white)
function getFontColorFromBackgroundColor (bgColor) {
if (!bgColor.match(/#[0-9a-z]{6}/i)) {
return 'white';
}
const [r, g, b] = [1, 3, 5].map((o) => parseInt(bgColor.slice(o, o + 2), 16));
const greyscale = (0.2125 * r) + (0.7154 * g) + (0.0721 * b);
return (greyscale > 127) ? 'black' : 'white';
}