rainbow_colors 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rainbow_colors/image_palette.rb +26 -4
- data/lib/rainbow_colors/version.rb +1 -1
- data/server/server.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97eb58ce4b49c9cc24e4388c64c5384dbaba54c9
|
4
|
+
data.tar.gz: 2c4962729f215682f31535a6585216c23dd269cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56d926650b9d6b5b7cb1316c175d6c627d095c1494ce563edd9653d1831b547efdb725c8ad907cf4d449c1836716c5d29c330c9a382a7ec445b1e5d7abd443bd
|
7
|
+
data.tar.gz: 1a0a77df341214e6bec280f11f9a753a4cd08140973a8e479bcd8528c0e9e5159cfe204f0b5535f784756c5cb96e1c8164b8864f7be1e4f9e0bb0b6e6c862209
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module RainbowColors
|
2
2
|
class ImagePalette
|
3
3
|
def initialize(image_path)
|
4
|
-
@
|
4
|
+
@image = Magick::ImageList.new(image_path).quantize(2048, Magick::RGBColorspace)
|
5
5
|
end
|
6
6
|
|
7
7
|
def scheme
|
@@ -45,24 +45,46 @@ module RainbowColors
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def color_background
|
48
|
+
# Pick the most-used color in the image
|
48
49
|
RainbowColors::Convertor.hex_from_rgb image_colors.first
|
49
50
|
end
|
50
51
|
|
51
52
|
def color_text
|
52
53
|
background_rgb = RainbowColors::Convertor.rgb_from_hex color_background
|
54
|
+
# http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
|
53
55
|
background_brightness = Math.sqrt(
|
54
56
|
background_rgb[:red] ** 2 * 0.241 +
|
55
57
|
background_rgb[:green] ** 2 * 0.691 +
|
56
58
|
background_rgb[:blue] ** 2 * 0.068
|
57
59
|
)
|
58
|
-
background_brightness <
|
60
|
+
background_brightness < 145 ? "#E6E6E6" : "#1A1A1A"
|
61
|
+
end
|
62
|
+
|
63
|
+
def color_accent
|
64
|
+
bg = RainbowColors::Convertor.rgb_from_hex color_background
|
65
|
+
color_scheme = scheme.map { |hex| RainbowColors::Convertor.rgb_from_hex(hex) }
|
66
|
+
|
67
|
+
contrast_colors = []
|
68
|
+
color_scheme.each do |c|
|
69
|
+
delta_red = [bg[:red], c[:red]].max - [bg[:red], c[:red]].min
|
70
|
+
delta_green = [bg[:green], c[:green]].max - [bg[:green], c[:green]].min
|
71
|
+
delta_blue = [bg[:blue], c[:blue]].max - [bg[:blue], c[:blue]].min
|
72
|
+
color_difference = delta_red + delta_green + delta_blue
|
73
|
+
|
74
|
+
contrast_colors << RainbowColors::Convertor.hex_from_rgb(c) if color_difference > 225
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return random contrasting color color, if available
|
78
|
+
contrast_colors.empty? ? color_text : contrast_colors.sample
|
59
79
|
end
|
60
80
|
|
61
81
|
private
|
62
82
|
|
63
83
|
def image_colors
|
64
|
-
|
65
|
-
colors = image.color_histogram.sort { |a,b| b[1] <=> a[1] }
|
84
|
+
# Sort colors from most-used to least-used
|
85
|
+
colors = @image.color_histogram.sort { |a,b| b[1] <=> a[1] }
|
86
|
+
|
87
|
+
# Generate RGB values for colors
|
66
88
|
colors.map! do |c|
|
67
89
|
red = c.first.red / 256
|
68
90
|
green = c.first.green / 256
|
data/server/server.rb
CHANGED