Skip to content

Instantly share code, notes, and snippets.

@linrock
linrock / rgb_to_yuv_and_back_to_rgb.py
Last active May 16, 2024 04:05
Color inaccuracies from converting 24-bit RGB -> YUV -> 24-bit RGB (BT.709)
""" This simulates converting 24-bit RGB values to YUV, then back to 24-bit RGB.
Using BT.709 transfer functions:
https://en.wikipedia.org/wiki/Rec._709
It demonstrates that converting to 30-bit YUV then back to 24-bit RGB is lossy.
Using 10 bits per YUV value appears to be lossless.
Converting RGB (24-bit) -> YUV (64-bit floats per channel, normalized [0-1]) -> RGB (24-bit)
Found 0 inaccurate conversions out of 16581375 RGB values
@linrock
linrock / capslock.cpp
Created January 16, 2020 03:00
Programmatically check if caps lock is on in macOS using C++
#include <IOKit/IOKitLib.h>
#include <IOKit/hidsystem/IOHIDLib.h>
#include <IOKit/hidsystem/IOHIDParameter.h>
#include <CoreFoundation/CoreFoundation.h>
// Prints 0 if capslock is off. Prints 1 if capslock is on
// Modified from: https://discussions.apple.com/thread/7094207
// Compile with:
// clang -framework IOKit -framework CoreFoundation -o capslock capslock.cpp
@linrock
linrock / stockfish.rb
Last active December 25, 2015 07:34
A simple ruby client for the Stockfish chess engine.
require 'open3'
module Stockfish
class InvalidCommand < StandardError; end
class InvalidOption < StandardError; end
class Engine
attr_reader :stdin, :stdout, :stderr, :wait_threads, :version, :pid
@linrock
linrock / backbone-demo.html
Last active December 11, 2015 22:28
Demo
<!DOCTYPE html>
<html>
<head>
<style>
body {
overflow: hidden;
font-family: Arial, sans-serif;
background: #222;
color: #eee;
margin: 0;
@linrock
linrock / 256colors.sh
Last active September 29, 2015 23:58
Print out 256-color terminal color codes in their respective colors
#!/bin/bash
for i in {0..15}; do
row=
for j in {1..16}; do
color=$(( $i*16 + $j -1 ))
code="\033[00;38;5;${color}m${color}"
if [[ $row == "" ]]; then
row=$code
elif [[ $color -lt 11 ]]; then
@linrock
linrock / alexa.sh
Last active January 6, 2016 08:04
Fetch the alexa rank of a domain
#!/bin/bash
curl -s "http://www.alexa.com/siteinfo/$1" | grep -Po "^([\d,]+)\s+"