This repository has been archived by the owner on Mar 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient.rb
executable file
·151 lines (130 loc) · 4.24 KB
/
gradient.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#####################################################################################
# gradient.rb 11.29.2014
#
# Ruby Gradient Generator
# author: Jeff Miller
# website: humani.se
# github: github.com/ibanez270dx
#
# USAGE:
# Enter RGB or Hex color values and number of steps
#
# yellow = [ 255, 246, 0 ]
# orange = "#ff9000"
# pink = [ 255, 56, 192 ]
# violet = "#29aa59"
#
# gradient = Gradient.new(colors: [ yellow, orange, pink, violet ], steps: 100)
# gradient.print
#
#####################################################################################
class Gradient
def initialize(options)
options = { steps: 10 }.merge(options)
@steps = options[:steps]-1 # Minus one to make room for the final breakpoint
@colors = options[:colors].collect { |color| to_rgb(color) }
@gradient_count = @colors.length-1 # Number of gradients to calculate
@substeps = @steps / @gradient_count # Substeps per gradient
@remainder = @steps % @gradient_count # Remaining steps
end
def generate
@gradient_count.times.inject([]) do |memo, index|
steps = @substeps
if @remainder > 0 # Add a step if there are leftovers still
steps += 1
@remainder -= 1
end
memo += gradient_for(@colors[index], @colors[index+1], steps)
memo
end.push(@colors.last)
end
def hex
# Returns an array of hex color values
generate.collect do |color|
to_hex(color)
end
end
def rgb
# Returns an array of RGB value arrays
generate.collect do |color|
color.collect(&:to_i)
end
end
def print
puts "Breakpoints:"
@colors.each do |color|
puts " #{color[0]}, #{color[1]}, #{color[2]}"
end
puts "\nIn #{@steps+1} Steps:"
generate.each_with_index do |color, i|
puts " #{i+1} :: #{to_hex(color)} :: #{color[0].to_i}, #{color[1].to_i}, #{color[2].to_i} #{' (breakpoint)' if @colors.include?(color)}"
end
end
private
def gradient_for(color1, color2, steps)
# Calculate a single color-to-color gradient
steps.times.inject([]) do |memo, index|
ratio = index.to_f / steps
r = color2[0] * ratio + color1[0] * (1 - ratio)
g = color2[1] * ratio + color1[1] * (1 - ratio)
b = color2[2] * ratio + color1[2] * (1 - ratio)
memo.push [ r, g, b ]
memo
end
end
def is_rgb?(color)
# Returns true for the form [ 123, 123, 123 ]
color.is_a?(Array) && color.length==3 && !color.collect{ |c| !!(c.to_i.to_s=~/^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$/) }.include?(false)
end
def is_hex?(color)
# Returns true for the form "#ffe100"
color.is_a?(String) && !!(color=~/^#[a-fA-F0-9]{6}$/)
end
def to_rgb(color)
# Converts color to an RGB value
case
when is_rgb?(color) then color
when is_hex?(color) then [ color[1..2].hex, color[3..4].hex, color[5..6].hex ]
else raise "#{color.inspect} is not a valid RGB or Hex color"
end
end
def to_hex(color)
# Converts color to a Hex value
case
when is_hex?(color) then color
when is_rgb?(color)
color.inject("#") do |memo, value|
str = value.to_i.to_s(16)
hex = str.length==1 ? str.concat(str) : str
memo.concat(hex)
memo
end
else raise "#{color.inspect} is not a valid RGB or Hex color"
end
end
end
# class Gradient
# attr_accessor :resolution, :R0, :G0, :B0, :R1, :G1, :B1
# def initialize(start, stop, resolution)
# @resolution = Float(resolution)
# @R0 = (start & 0xff0000) >> 16;
# @G0 = (start & 0x00ff00) >> 8;
# @B0 = (start & 0x0000ff) >> 0;
# @R1 = (stop & 0xff0000) >> 16;
# @G1 = (stop & 0x00ff00) >> 8;
# @B1 = (stop & 0x0000ff) >> 0;
# end
# def gradient(step)
# r = interpolate(@R0, @R1, step);
# g = interpolate(@G0, @G1, step);
# b = interpolate(@B0, @B1, step);
# (((r << 8) | g) << 8) | b;
# end
# def interpolate(start, stop, step)
# if (start < stop)
# return (((stop - start) * (step / @resolution)) + start).round;
# else
# return (((start - stop) * (1 - (step / @resolution))) + stop).round;
# end
# end
# end