Skip to content

Instantly share code, notes, and snippets.

View tobiashm's full-sized avatar

Tobias H. Michaelsen tobiashm

View GitHub Profile
@tobiashm
tobiashm / gist:985689
Created May 22, 2011 17:27
xkcd show image title bookmarklet
javascript:(function(){var i=document.querySelector('#comic img'),t=i.getAttribute('title');i.insertAdjacentText('afterEnd',t);})();
-- initial
UPDATE item SET depth = 0 WHERE parent_item_guid IS NULL;
-- SQLite3 doesn't support loops, so run this until `select count(*) from item where product_code is null` = 0
INSERT OR REPLACE INTO item(item_guid, user_guid, parent_item_guid, name, depth)
SELECT t.item_guid, t.user_guid, t.parent_item_guid, t.name, p.depth + 1 FROM item AS t
INNER JOIN item AS p ON (t.parent_item_guid = p.item_guid)
WHERE p.depth >= 0
AND NOT p.item_guid IS NULL
AND t.depth IS NULL;
@tobiashm
tobiashm / erb-encoding-test.rb
Created October 14, 2011 15:26
Test encoding in ERB
# encoding: UTF-8
require 'erb'
puts __ENCODING__
puts ERB.new('<%= __ENCODING__ %>').result
@tobiashm
tobiashm / contrast.js
Last active October 15, 2021 02:02
Calculate contrasting colors, using https://github.com/One-com/one-color
var contrast = (function () {
var MIN_CONTRAST_RATE_AA = { normal: 4.5, large: 3 },
MIN_CONTRAST_RATE_AAA = { normal: 7, large: 4.5 },
MIN_BRIGHT_DIFF = 125/255,
MIN_COLOR_DIFF = 500/255,
BRIGHTNESS_COEFS = [0.299, 0.587, 0.114],
LUMINANCE_COEFS = [0.2126, 0.7152, 0.0722];
function abs_diff(array, other) {
@tobiashm
tobiashm / sass-yuv.gemspec
Last active September 30, 2015 00:47
SASS YUV color space extensions
Gem::Specification.new do |spec|
spec.name = 'sass-yuv'
spec.version = '0.1.0'
spec.platform = Gem::Platform::RUBY
spec.author = 'Tobias H. Michaelsen'
spec.email = '[email protected]'
spec.summary = 'YUV color space in SASS'
spec.description = 'A simple extension to SASS that alows you to convert to and from the YUV color space, and adjust brightness.'
spec.homepage = 'https://gist.github.com/1694554'
spec.license = 'MIT'
module ModelSerializer
def self.serialize(object)
object.to_json include: nil # don't include any associations - only serialize shallow object
end
def self.deserialize(json)
hash = ActiveSupport::JSON.decode(json) # => { "section": { "id": 42, ... } }
model = hash.keys.first.camelize.constantize # => Section
attributes = hash.values.first
object = model.new
@tobiashm
tobiashm / inline-color-image.rb
Created April 30, 2012 06:28
SASS inline-color-image
require "sass"
require "chunky_png"
module Sass::Script::Functions
# Generates a data-url for a PNG created from the given color.
# Can be used to set a alpha-transparent background for IE8<
#
# @example
# background: url(inline-color-image(rgba(102, 54, 32, 0.5)));
def inline_color_image(color)
@tobiashm
tobiashm / gist:2560372
Created April 30, 2012 17:41
Computer math WTF
Math.round(0.4999999999999999722444243843710865) == 1
Math.round(0.4999999999999999722444243843710864) == 0
APP_PATH = File.expand_path('../..', __FILE__)
require File.join(APP_PATH, "lib/site")
working_directory APP_PATH
pid_path = File.join(APP_PATH, "tmp/pids/web.pid")
pid pid_path
stderr_path File.join(APP_PATH, "log/unicorn-stderr")
stdout_path File.join(APP_PATH, "log/unicorn-stdout")
@tobiashm
tobiashm / sass-inline-color-image.rb
Created August 20, 2012 13:53
Generates a data-url for a PNG created from the given color.
require "chunky_png"
module Sass::Script::Functions
# Generates a data-url for a PNG created from the given color.
# Can be used to set a alpha-transparent background for IE8<
#
# @example
# background: url(inline-color-image(rgba(102, 54, 32, 0.5)));
def inline_color_image(color)
assert_type color, :Color