kiso 0.5.2.pre → 0.5.3.pre
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/app/views/kiso/components/_avatar.html.erb +2 -2
- data/lib/kiso/color_utils.rb +11 -7
- data/lib/kiso/configuration.rb +11 -0
- data/lib/kiso/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0e2ae8ad60d92d8f26f348cfcf1dc7e244ab9d43d8650c70bb55563a569ebd92
|
|
4
|
+
data.tar.gz: 75bcf4fd1147c79754256ab6c3cccd994a1b5a7ace087fd52529e716d7521422
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 80ddd9d1a892eec79083c535516369d0278c3680ee75fd9e7cbffb406a8684d705805104ee12dd2e8d5a920baa15aa1a445d31c637d0ec8a8a675b05889f96e6
|
|
7
|
+
data.tar.gz: '06824eba6296399c458259426d806da86901040d0922c696efc1f066e0c1feed2c8f405fb09edc5344e54995ef51a932e313291ee77846605315d3e8c433f1ca'
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
<%# locals: (src: nil, alt: "", text: nil, size: :md, color: nil, ui: {}, css_classes: "", **component_options) %>
|
|
2
|
-
<% text_color = Kiso::ColorUtils.contrast_text_color(color) if color %>
|
|
1
|
+
<%# locals: (src: nil, alt: "", text: nil, size: :md, color: nil, contrast_threshold: nil, ui: {}, css_classes: "", **component_options) %>
|
|
2
|
+
<% text_color = Kiso::ColorUtils.contrast_text_color(color, threshold: contrast_threshold) if color %>
|
|
3
3
|
<%= content_tag :span,
|
|
4
4
|
class: Kiso::Themes::Avatar.render(size: size, class: css_classes),
|
|
5
5
|
style: (color ? "background-color: #{color}; color: #{text_color};" : nil),
|
data/lib/kiso/color_utils.rb
CHANGED
|
@@ -4,13 +4,17 @@ module Kiso
|
|
|
4
4
|
module ColorUtils
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
|
+
DEFAULT_CONTRAST_THRESHOLD = 0.42
|
|
8
|
+
|
|
7
9
|
# Returns "white" or "black" based on WCAG relative luminance.
|
|
8
|
-
# Uses a perceptual threshold
|
|
9
|
-
# midpoint of 0.179, per Lea Verou's research on
|
|
10
|
-
# generation. The higher threshold produces better
|
|
11
|
-
# saturated chromatic colors (e.g. Tailwind 500-shade
|
|
12
|
-
# Accepts 3-digit (#abc) or 6-digit (#aabbcc) hex strings.
|
|
13
|
-
def contrast_text_color(hex)
|
|
10
|
+
# Uses a perceptual threshold (default 0.42) rather than the
|
|
11
|
+
# mathematical midpoint of 0.179, per Lea Verou's research on
|
|
12
|
+
# contrast color generation. The higher threshold produces better
|
|
13
|
+
# results on saturated chromatic colors (e.g. Tailwind 500-shade
|
|
14
|
+
# palette). Accepts 3-digit (#abc) or 6-digit (#aabbcc) hex strings.
|
|
15
|
+
def contrast_text_color(hex, threshold: nil)
|
|
16
|
+
threshold ||= Kiso.config.contrast_threshold
|
|
17
|
+
|
|
14
18
|
hex = hex.delete("#")
|
|
15
19
|
hex = hex.chars.map { |c| c * 2 }.join if hex.length == 3
|
|
16
20
|
|
|
@@ -19,7 +23,7 @@ module Kiso
|
|
|
19
23
|
(c <= 0.04045) ? c / 12.92 : ((c + 0.055) / 1.055)**2.4
|
|
20
24
|
}.then { |lr, lg, lb| 0.2126 * lr + 0.7152 * lg + 0.0722 * lb }
|
|
21
25
|
|
|
22
|
-
(luminance >
|
|
26
|
+
(luminance > threshold) ? "black" : "white"
|
|
23
27
|
end
|
|
24
28
|
end
|
|
25
29
|
end
|
data/lib/kiso/configuration.rb
CHANGED
|
@@ -33,10 +33,21 @@ module Kiso
|
|
|
33
33
|
# end
|
|
34
34
|
attr_accessor :app_theme
|
|
35
35
|
|
|
36
|
+
# @return [Float] luminance threshold for contrast text color calculation.
|
|
37
|
+
# Colors with luminance above this value get black text, below get white.
|
|
38
|
+
# Default is 0.42 (perceptual midpoint per Lea Verou's research).
|
|
39
|
+
#
|
|
40
|
+
# @example Tune for a darker palette
|
|
41
|
+
# Kiso.configure do |config|
|
|
42
|
+
# config.contrast_threshold = 0.36
|
|
43
|
+
# end
|
|
44
|
+
attr_accessor :contrast_threshold
|
|
45
|
+
|
|
36
46
|
def initialize
|
|
37
47
|
@icons = default_icons
|
|
38
48
|
@theme = {}
|
|
39
49
|
@app_theme = :default
|
|
50
|
+
@contrast_threshold = Kiso::ColorUtils::DEFAULT_CONTRAST_THRESHOLD
|
|
40
51
|
end
|
|
41
52
|
|
|
42
53
|
# Resolves the active app theme directory path relative to the given root.
|
data/lib/kiso/version.rb
CHANGED