is_dark 0.1.0
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.
Potentially problematic release.
This version of is_dark might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/lib/is_dark.rb +118 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a04b8eb2962449d2a8d7ba8060203481e88d6eaaacf07afb2783e0d811f06357
|
4
|
+
data.tar.gz: cd9aa4e9690dd089adee3af8acff26337a1b04409f6af39f1d816865251e04bb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1fc12a6110c52a8d3f408d57f07e2e8ad3a37e330349353ac8abb39f3b6cc2104187b8724a40161be122dc5b200e938aef179afa64aaad4735a89cd499a34635
|
7
|
+
data.tar.gz: 9e1a0d6cbe879f1620c420f0dc12df1d1fe19fa24c7388bbceea7dbf29c8b8eaa0f789cef20904f65528b60398619b75af14de5fbfcdaa397de96b738632d1ad
|
data/lib/is_dark.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rmagick'
|
4
|
+
class IsDark
|
5
|
+
@r = 0
|
6
|
+
@g = 0
|
7
|
+
@b = 0
|
8
|
+
@colorset = 255
|
9
|
+
@with_debug = false
|
10
|
+
@with_debug_file = false
|
11
|
+
@debug_file_path = '/tmp/is_dark_debug_file.pdf'
|
12
|
+
|
13
|
+
def self.color(hex)
|
14
|
+
@r, @g, @b = hex.match(/^#(..)(..)(..)$/).captures.map(&:hex)
|
15
|
+
@colorset = 255
|
16
|
+
is_dark
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.magick_pixel(pix, x = nil, y = nil, set_not_detected_light = true)
|
20
|
+
@r = pix.red.to_f
|
21
|
+
@g = pix.green.to_f
|
22
|
+
@b = pix.blue.to_f
|
23
|
+
@colorset = 655 * 255
|
24
|
+
is_dark(x, y, set_not_detected_light)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.magick_pixel_from_blob(x, y, blob, _set_not_detected_light = true)
|
28
|
+
image = Magick::Image.read(blob).first
|
29
|
+
pix = image.pixel_color(x, y)
|
30
|
+
magick_pixel(pix, x, y)
|
31
|
+
end
|
32
|
+
|
33
|
+
# (x, y) is the left corner of an element over a blob, height and width is the element's size
|
34
|
+
def self.magick_area_from_blob(x, y, blob, height, width, percent = 80, range = (0..10), set_not_detected_light = true)
|
35
|
+
@set_not_detected_light = set_not_detected_light
|
36
|
+
image = Magick::Image.read(blob).first
|
37
|
+
dark = false
|
38
|
+
dots = []
|
39
|
+
range.each do |xx|
|
40
|
+
range.each do |yy|
|
41
|
+
dots << { 'x': (x + width * xx / 10).to_i, 'y': (y + height * yy / 10).to_i }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
points = 0
|
46
|
+
if @with_debug_file
|
47
|
+
oldx = false
|
48
|
+
oldy = false
|
49
|
+
end
|
50
|
+
p '====================================================================' if @with_debug
|
51
|
+
dots.each do |dot|
|
52
|
+
x = dot[:x].to_i
|
53
|
+
y = dot[:y].to_i
|
54
|
+
pix = image.pixel_color(x, y)
|
55
|
+
l = magick_pixel(pix, x, y, set_not_detected_light)
|
56
|
+
points += 1 if l
|
57
|
+
next unless @with_debug_file
|
58
|
+
|
59
|
+
draw_debug_files(image, x, y, oldx, oldy)
|
60
|
+
oldy = y
|
61
|
+
oldx = x
|
62
|
+
end
|
63
|
+
dark = true if points >= (dots.length / 100) * percent
|
64
|
+
if @with_debug
|
65
|
+
p '===================================================================='
|
66
|
+
p "Total Points: #{dots.length}, dark points amount:#{points}"
|
67
|
+
p "Is \"invert to white not detectd pixels\" option enabled?:#{set_not_detected_light}"
|
68
|
+
p "Signature will be inverted if #{percent}% of dots will be dark"
|
69
|
+
p "Is inverted?: #{dark}"
|
70
|
+
p '===================================================================='
|
71
|
+
end
|
72
|
+
dark
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.set_debug_data(show_info = false, draw_file = false)
|
76
|
+
@with_debug = show_info
|
77
|
+
return unless draw_file != false
|
78
|
+
|
79
|
+
@with_debug_file = true
|
80
|
+
@debug_file_path = draw_file
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.draw_debug_files(image, x, y, oldx, oldy)
|
84
|
+
return unless oldx && oldy
|
85
|
+
|
86
|
+
gc = Magick::Draw.new
|
87
|
+
gc.line(x, y, oldx, oldy)
|
88
|
+
gc.stroke('black')
|
89
|
+
gc.draw(image)
|
90
|
+
image.write(@debug_file_path)
|
91
|
+
end
|
92
|
+
|
93
|
+
# detects a dark color based on luminance W3 standarts ( https://www.w3.org/TR/WCAG20/#relativeluminancedef )
|
94
|
+
def self.is_dark(x = nil, y = nil, set_not_detected_light = true)
|
95
|
+
dark = false
|
96
|
+
inverted = false
|
97
|
+
pixel = [@r.to_f, @g.to_f, @b.to_f]
|
98
|
+
if set_not_detected_light && pixel[0] == 0.0 && pixel[1] == 0.0 && pixel[2] == 0.0 # probably not detected pixel color by Imagick, will be considered as "white" if "set_not_detected_light = true"
|
99
|
+
pixel = [65_535.0, 65_535.0, 65_535.0]
|
100
|
+
inverted = true
|
101
|
+
end
|
102
|
+
calculated = []
|
103
|
+
pixel.each do |color|
|
104
|
+
color /= @colorset
|
105
|
+
color /= 12.92 if color <= 0.03928
|
106
|
+
color = ((color + 0.055) / 1.055)**2.4 if color > 0.03928
|
107
|
+
calculated << color
|
108
|
+
end
|
109
|
+
l = (0.2126 * calculated[0] + 0.7152 * calculated[1] + 0.0722 * calculated[2])
|
110
|
+
dark = true if l <= 0.05
|
111
|
+
if @with_debug
|
112
|
+
debug = { 'X': x, 'Y': y, 'R': @r, 'G': @g, 'B': @b, 'luminance value': l, 'is_dark': dark,
|
113
|
+
'inverted to white': inverted }
|
114
|
+
p debug
|
115
|
+
end
|
116
|
+
dark
|
117
|
+
end
|
118
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: is_dark
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergei Illarionov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rmagick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
description: "Detects a dark color based on luminance W3 standarts ( https://www.w3.org/TR/WCAG20/#relativeluminancedef
|
28
|
+
). \n\n It has these options: \n * is a hex color dark \n * is an Imagick pixel
|
29
|
+
dark \n * is an Imagick pixel from a blob dark \n * is an area in a blob over a
|
30
|
+
dark background (uses Imagick for it too). \n\n An example practical aspect: it
|
31
|
+
can be useful to understand will a black colored text be visible or not over an
|
32
|
+
area."
|
33
|
+
email: butteff.ru@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- lib/is_dark.rb
|
39
|
+
homepage: https://butteff.ru/en/
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
source_code_uri: https://github.com/butteff/is_dark_ruby_gem
|
44
|
+
post_install_message: You can find docs about is_dark gem on https://butteff.ru/en/extensions.html
|
45
|
+
or on https://github.com/butteff/is_dark_ruby_gem
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.7.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.4.19
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Detects a dark background under an area or by a color code
|
64
|
+
test_files: []
|