Skip to content

Instantly share code, notes, and snippets.

View jfsiii's full-sized avatar

John Schulz jfsiii

View GitHub Profile
@jfsiii
jfsiii / relativeLuminanceW3C.js
Last active April 3, 2024 01:07
Calculate the relative luminance, or brightness, of a color. Accepts values 0-255 for R, G, B.
// from http://www.w3.org/TR/WCAG20/#relativeluminancedef
function relativeLuminanceW3C(R8bit, G8bit, B8bit) {
var RsRGB = R8bit/255;
var GsRGB = G8bit/255;
var BsRGB = B8bit/255;
var R = (RsRGB <= 0.03928) ? RsRGB/12.92 : Math.pow((RsRGB+0.055)/1.055, 2.4);
var G = (GsRGB <= 0.03928) ? GsRGB/12.92 : Math.pow((GsRGB+0.055)/1.055, 2.4);
var B = (BsRGB <= 0.03928) ? BsRGB/12.92 : Math.pow((BsRGB+0.055)/1.055, 2.4);
@jfsiii
jfsiii / fetch-chunked.js
Last active March 30, 2024 01:37
Quick example of using fetch to parse a chunked response
var chunkedUrl = 'https://jigsaw.w3.org/HTTP/ChunkedScript';
fetch(chunkedUrl)
.then(processChunkedResponse)
.then(onChunkedResponseComplete)
.catch(onChunkedResponseError)
;
function onChunkedResponseComplete(result) {
console.log('all done!', result)
}
@jfsiii
jfsiii / img2data.js
Created January 31, 2011 15:54
base64 encoding images in NodeJS
/*
* Complete reworking of JS from https://gist.github.com/803410
* Removes external `request` dependency
* Caveats:
* * No error checking
* * Largely a POC. `data` URI is accurate, but this code cannot simply be inserted into an `express` app
*/
var URL = require('url'),
sURL = 'http://nodejs.org/logo.png',
oURL = URL.parse(sURL),
@jfsiii
jfsiii / relativelumance.js
Last active March 28, 2022 14:27
Relative Luminance from RGB color.
// from http://en.wikipedia.org/wiki/Luminance_(relative)
// R, G, B are integers between 0 and 255
function relativeLuminance(R, G, B) {
var Y = (0.2126 * R + 0.7152 * G + 0.0722 * B) / 255;
return Y;
}
@jfsiii
jfsiii / index.html
Last active December 21, 2021 18:45
<!doctype html>
<body>
<script>
// based on https://github.com/paulirish/memory-stats.js/blob/6fa0f359134c4668bd9b931b95156bae1073e8eb/memory-stats.js#L70-L79
function bytesToSize( bytes, nFractDigit ){
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return 'n/a'
nFractDigit = nFractDigit !== undefined ? nFractDigit : 0
var precision = Math.pow(10, nFractDigit)
var i = Math.floor(Math.log(bytes) / Math.log(1024))
@jfsiii
jfsiii / README.md
Created June 6, 2016 20:27
A New Pattern for Updatable D3.js Charts

Using a new updatable chart format. Update functions are made accessible to the caller, handing over chart controls with full functionality to the caller in a modular manner. Data binding is done with method chaining, like any other configuration variable, and can be changed after initialization. This allows for changes to be rendered in the context of chart history, leveraging D3's transitions and update logic.

forked from rcmoore38's block: A New Pattern for Updatable D3.js Charts

@jfsiii
jfsiii / request.js
Created September 17, 2012 19:26
Give a url, get back a base64-encoded data URI
/**
* Given an image URL, return a base64-encoded data URI.
* @param {String} url|uri|image The URL for the image to be converted.
* @param {String} [callback|cb] Name of the function you want to be called.
* @returns {Object} JSON
* @returns {String} JSON.data The data URI for the image
* @returns {Object} JSON.meta
* @returns {Number} JSON.meta.width The width of the image, in pixels.
* @returns {Number} JSON.meta.height The height of the image, in pixels.
* @returns {Object} JSON.http
@jfsiii
jfsiii / index.html
Last active January 4, 2021 18:11
Full-Screen Layout using Flexbox
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Holy Grail</title>
<style>
/* some basic styles. nothing to do with flexbox */
header, footer,
nav, article, aside {
border: 1px solid black;
{
"version" : "2.0",
"id" : [ "Date", "Country", "Client", "bin_start" ],
"size" : [ 28, 1, 1, 191 ],
"label" : "Time to First Tweet — Cold Start (With Em Dash)",
"dimension" : {
"Date" : {
"category" : {
"index" : {
"2016-09-21T00:00:00Z": 0,
{
"id": [
"bin_start",
"tool",
"feature",
"day"
],
"dimension": {
"bin_start": {
"label": "Bin",