Skip to content

Commit ce01d8d

Browse files
committed
Implement mix and mixp
1 parent da0cd21 commit ce01d8d

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

lib/glug.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ module Glug # :nodoc:
1111
require_relative 'glug/layer'
1212
require_relative 'glug/extensions'
1313
require_relative 'glug/hsluv_modifiers'
14+
require_relative 'glug/chroma_extensions'
1415
require_relative 'glug/stylesheet_dsl'

lib/glug/chroma_extensions.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require 'chroma'
4+
require 'hsluv'
5+
6+
module Chroma
7+
class Color # :nodoc:
8+
def mix(other, weight = 50)
9+
other = other.paint if other.is_a?(String)
10+
p = weight / 100.0
11+
12+
r = (rgb.r * p + other.rgb.r * (1 - p)).round
13+
g = (rgb.g * p + other.rgb.g * (1 - p)).round
14+
b = (rgb.b * p + other.rgb.b * (1 - p)).round
15+
16+
Chroma.paint("rgb(#{r}, #{g}, #{b})")
17+
end
18+
end
19+
end

lib/glug/extensions.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55

66
# Colour methods on Integer
77
class Integer
8-
# rubocop:disable Style/StringConcatenation, Naming/MethodParameterName
9-
def chroma_hex(op, p = nil)
8+
# rubocop:disable Style/StringConcatenation
9+
def chroma_hex(op, *args)
1010
color = ('#' + to_s(16).rjust(6, '0')).paint
11-
(p.nil? ? color.send(op) : color.send(op, p)).to_hex
11+
color.send(op, *args).to_hex
1212
end
1313

14-
def chroma(op, p = nil)
15-
chroma_hex(op, p).gsub('#', '0x').to_i(16)
14+
def chroma(op, *args)
15+
chroma_hex(op, *args).gsub('#', '0x').to_i(16)
1616
end
1717

1818
def to_hex_color
1919
'#' + to_s(16).rjust(6, '0')
2020
end
21-
# rubocop:enable Style/StringConcatenation, Naming/MethodParameterName
21+
# rubocop:enable Style/StringConcatenation
2222
end
2323

2424
# Top-level colour generators

lib/glug/hsluv_modifiers.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,25 @@ def spinp(amount = 0)
4747
rgb_values = Hsluv.rgb_prepare(Hsluv.hsluv_to_rgb(new_h, s, l))
4848
Chroma.paint("rgb(#{rgb_values[0]}, #{rgb_values[1]}, #{rgb_values[2]})")
4949
end
50+
51+
def mixp(other, weight = 50)
52+
other = other.paint if other.is_a?(String)
53+
p = weight / 100.0
54+
55+
h1, s1, l1 = Hsluv.rgb_to_hsluv(rgb.r / 255.0, rgb.g / 255.0, rgb.b / 255.0)
56+
h2, s2, l2 = Hsluv.rgb_to_hsluv(other.rgb.r / 255.0, other.rgb.g / 255.0, other.rgb.b / 255.0)
57+
58+
# Interpolate hue on the shortest path around the color wheel
59+
h_diff = h2 - h1
60+
h_diff -= 360 if h_diff > 180
61+
h_diff += 360 if h_diff < -180
62+
new_h = (h1 + h_diff * (1 - p)) % 360
63+
64+
new_s = s1 * p + s2 * (1 - p)
65+
new_l = l1 * p + l2 * (1 - p)
66+
67+
rgb_values = Hsluv.rgb_prepare(Hsluv.hsluv_to_rgb(new_h, new_s, new_l))
68+
Chroma.paint("rgb(#{rgb_values[0]}, #{rgb_values[1]}, #{rgb_values[2]})")
69+
end
5070
end
5171
end

spec/fixtures/color_functions.glug

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ end
4949
layer(:spinp, source: :shortbread) do
5050
line_color 0x998877.chroma(:spinp, 20)
5151
end
52+
53+
layer(:mix, source: :shortbread) do
54+
line_color 0x998877.chroma(:mix, '#ffffff', 50)
55+
end
56+
57+
layer(:mixp, source: :shortbread) do
58+
line_color 0x998877.chroma(:mixp, '#ffffff', 50)
59+
end

spec/fixtures/color_functions.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@
9191
"id":"spinp",
9292
"source-layer":"spinp",
9393
"type":"line"
94+
},
95+
{
96+
"paint":{"line-color":"#ccc4bb"},
97+
"source":"shortbread",
98+
"id":"mix",
99+
"source-layer":"mix",
100+
"type":"line"
101+
},
102+
{
103+
"paint":{"line-color":"#cdc1be"},
104+
"source":"shortbread",
105+
"id":"mixp",
106+
"source-layer":"mixp",
107+
"type":"line"
94108
}
95109
]
96110
}

0 commit comments

Comments
 (0)