Skip to content

Instantly share code, notes, and snippets.

View creasty's full-sized avatar

Yuki Iwanaga creasty

View GitHub Profile
@creasty
creasty / hsl2rgb.rb
Created May 29, 2015 07:56
HSL to RGB
c = (1 - (2 * l - 1).abs) * s
x = c * (1 - ((h / 60.0) % 2 - 1).abs)
m = l - c / 2
rgb = [
[c, x, 0],
[x, c, 0],
[0, c, x],
[0, x, c],
[x, 0, c],
@creasty
creasty / gist:a1f2fe574cb00f864504
Created January 19, 2015 15:06
Change all past commiters/authors in git
git filter-branch -f --env-filter '
if [ "$GIT_COMMITTER_EMAIL" != "yuki@creasty.com" ]; then
export GIT_AUTHOR_NAME="nondisclosure"
export GIT_AUTHOR_EMAIL="nondisclosure@creasty.com"
export GIT_COMMITTER_NAME="nondisclosure"
export GIT_COMMITTER_EMAIL="nondisclosure@creasty.com"
fi
' --tag-name-filter cat -- --branches --tags
module Base32
ENCODE_MAP = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
DECODE_MAP = Hash[
ENCODE_MAP
.each_char
.zip(0..31)
.concat([
['I', 1],
@creasty
creasty / suppress_status.rb
Created September 13, 2014 14:44
Suppress status code in Rails
module Concerns
module SuppressStatus
extend ActiveSupport::Concern
included do
protected :suppress_status!
protected :suppress_status?
prepend_after_action :suppress_status!
@creasty
creasty / gist:096553c0f157b6ced528
Created August 27, 2014 08:47
Self made SSL certification

Simplest way

openssl genrsa 2048 > cert.key
openssl req -new -batch -key cert.key > cert.csr
openssl x509 -days 3650 -req -signkey cert.key < cert.csr > cert.crt  # valid for a decade (3650 days)

Secure way

@creasty
creasty / gist:fec4a64b53c47185a6e1
Last active August 29, 2015 14:05
Simple popularity calculation algorithm
@creasty
creasty / weighted_random.coffee
Created July 15, 2014 14:35
Generate random numbers with weighted probability
weightedRandom = (spec) ->
len = spec.length
totalWeight = 0
totalWeight += (spec[i] ?= (1 / len)) for i in [0...len] by 1
rand = Math.random() * totalWeight
weightSum = 0
for i in [0...len] by 1
weightSum += spec[i]
return i if rand <= weightSum
@creasty
creasty / gist:4285b71bfe76c98c5be3
Created July 5, 2014 13:16
Convert timezone to Japan's in MySQL
select @@global.time_zone, now(), convert_tz(now(), 'utc', '+09:00');
@creasty
creasty / color_extract.rb
Last active April 10, 2017 07:53
Extract main colors from image in ruby
require 'rmagick'
class ColorExtraction
DEFAULT_QUANTIZER = 150
def self.from_file(file, quantize: DEFAULT_QUANTIZER)
new(Magick::ImageList.new(file), quantize: quantize)
end