Skip to content

Instantly share code, notes, and snippets.

View hsingtism's full-sized avatar

Hsing Lo hsingtism

  • Georgia, United States
  • 16:46 (UTC -04:00)
View GitHub Profile
const digitKey = [
"","I","II","III","IV","V","VI","VII","VIII","IX",
"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
"","M","MM","MMM"
]
const arabicToRoman = n => n.toString().padStart(4, '0').split('').map((v, i) => digitKey[+v + (3 - i) * 10]).join('')
arabicToRoman.validate = n => Number.isInteger(n) && n > 0 && n < 4000
const useBlack = (r, g, b) => [r, g, b]
.map((v, i) => v < 11
? v / [15496, 4606, 45631][i]
: Math.pow(v / 255 + 0.055, 2.4) / [5.348631, 1.589931, 15.74957][i])
.reduce((a, s) => a + s, 0)
> 0.17912878
const noteToHz = note => 440 * 2 ** (
("C-D-EF-G-A-B".indexOf(note[0]) + (note.match(/#/g) || []).length - (note.match(/b/g) || []).length - 9) / 12
+ (Number((note.match(/\d|-/g) || ['4']).join('')) - 4)
)
noteToHz.validate = s => /^[A-G][#b]*-?\d*$/g.test(s)
/*
Function that converts scientific pitch notation (American standard pitch notation or international pitch notation)
to a frequency. Input must pass noteToPitch.validate else function may not work as intended.
First character must be an uppercase letter of the note A-G. Follows is
'#' for sharp and 'b' for flat. Although not standard, these flat and sharp
modifiers may be stacked infinitely. The octave number follows; it can be any integer.
All pitch is equal temperament at A440. The pitch of standard A can be trivially
changed.
Example:
@hsingtism
hsingtism / randomFP.c
Created January 16, 2024 01:55
mask a random 64 bit integer to a floating point range
/*
random64() is a random integer function
this code uses undefined behavior but it'll be alright
make sure to include stdint
*/
#include <stdint.h>
// mask to (-2,2]
// this is just an example, change the exponent bits for other ranges
inline double _22() {
@hsingtism
hsingtism / xorshift128plus.c
Created January 16, 2024 01:51
pseudorandom number generator I use sometimes
// based on V8 https://v8.dev/blog/math-random.
uint64_t state0 = 0x4D696B696E6C6579;
uint64_t state1 = 0x0000004873696E67;
uint64_t xorshift128plus() {
uint64_t s1 = state0;
uint64_t s0 = state1;
state0 = s0;
s1 ^= s1 << 23;
s1 ^= s1 >> 17;
s1 ^= s0;
#include <stdio.h>
#define BYTES_PER_PIXEL 3
#define FILE_HEADER_SIZE 14
#define INFO_HEADER_SIZE 40
/*
Generates and saves a bitmap image.
- *image - ptr to array of 8 bit image data. unsigned char image[HEIGHT][WIDTH][BYTES_PER_PIXEL]
up to down > left to right > blue, green, red
@hsingtism
hsingtism / numToWords.js
Last active April 4, 2022 05:28
A function that convert integers to English words (short scale)
function numToWord(num) {
if (!Number.isSafeInteger(num) || num < 0) return null
if (!num) return 'zero '
const ntt = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen ']
let mstr = ''
let nArr = num.toString().padStart(18, '0').match(/.{1,3}/g)
for (let i = 5; i > -1; i--) {
let wkv = nArr.shift()
if (!+wkv) continue
if (+wkv[0]) mstr += ntt[+wkv[0]] + 'hundred '
@hsingtism
hsingtism / textColor.js
Last active April 16, 2024 01:03
A function that calculates if text color should be black given a background color. (W3C compliant for all values)
function useBlack(r, g, b) {
const e = v => Math.pow(v / 255 + 0.055, 2.4)
r < 11 ? r /= 15496 : r = e(r) / 5.348631
g < 11 ? g /= 4606. : g = e(g) / 1.589931
b < 11 ? b /= 45631 : b = e(b) / 15.74957
return r + g + b > 0.17912878
}