Skip to content

Instantly share code, notes, and snippets.

View nathforge's full-sized avatar

Nathan Reynolds nathforge

View GitHub Profile
@nathforge
nathforge / gist:604509
Created September 30, 2010 12:45
Manipulate RGB colours
class RGBColor(object):
def __init__(self, value):
"""
A representation of an RGB color, allowing color manipulation.
Can take values in the form of strings, integers,
three-element iterables, or another RGBColor object.
>>> print RGBColor('#FFF')
@nathforge
nathforge / gist:658336
Created November 1, 2010 15:22
Find the dominant colour in an image
import colorsys
def get_dominant_color(image):
"""
Find a PIL image's dominant color, returning an (r, g, b) tuple.
"""
image = image.convert('RGBA')
# Shrink the image, so we don't spend too long analysing color
@nathforge
nathforge / expbackoff.sh
Created December 22, 2016 10:33
Exponential backoff in Bash
expbackoff() {
# Exponential backoff: retries a command upon failure, scaling up the delay between retries.
# Example: "expbackoff my_command --with --some --args --maybe"
local MAX_RETRIES=${EXPBACKOFF_MAX_RETRIES:-8} # Max number of retries
local BASE=${EXPBACKOFF_BASE:-1} # Base value for backoff calculation
local MAX=${EXPBACKOFF_MAX:-300} # Max value for backoff calculation
local FAILURES=0
while ! "$@"; do
FAILURES=$(( $FAILURES + 1 ))
if (( $FAILURES > $MAX_RETRIES )); then