is_dark 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/is_dark.rb +92 -88
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a04b8eb2962449d2a8d7ba8060203481e88d6eaaacf07afb2783e0d811f06357
|
4
|
+
data.tar.gz: cd9aa4e9690dd089adee3af8acff26337a1b04409f6af39f1d816865251e04bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fc12a6110c52a8d3f408d57f07e2e8ad3a37e330349353ac8abb39f3b6cc2104187b8724a40161be122dc5b200e938aef179afa64aaad4735a89cd499a34635
|
7
|
+
data.tar.gz: 9e1a0d6cbe879f1620c420f0dc12df1d1fe19fa24c7388bbceea7dbf29c8b8eaa0f789cef20904f65528b60398619b75af14de5fbfcdaa397de96b738632d1ad
|
data/lib/is_dark.rb
CHANGED
@@ -1,114 +1,118 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rmagick'
|
2
4
|
class IsDark
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
10
18
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
16
26
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
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
|
23
32
|
|
24
|
-
|
25
|
-
|
26
|
-
pix = image.pixel_color(x, y)
|
27
|
-
self.magick_pixel(pix, x, y)
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.magick_area_from_blob(x, y, blob, height, width, percent=80, range=(0..10), set_not_detected_light=true) # (x, y) is the left corner of an element over a blob, height and width is the element's size
|
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)
|
31
35
|
@set_not_detected_light = set_not_detected_light
|
32
36
|
image = Magick::Image.read(blob).first
|
33
37
|
dark = false
|
34
38
|
dots = []
|
35
|
-
range.each
|
36
|
-
range.each
|
37
|
-
dots << {'x': (x+width*xx/10).to_i, 'y': (y+height*yy/10).to_i}
|
38
|
-
|
39
|
-
|
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
|
40
44
|
|
41
45
|
points = 0
|
42
46
|
if @with_debug_file
|
43
|
-
|
44
|
-
|
47
|
+
oldx = false
|
48
|
+
oldy = false
|
45
49
|
end
|
46
50
|
p '====================================================================' if @with_debug
|
47
|
-
dots.each
|
51
|
+
dots.each do |dot|
|
48
52
|
x = dot[:x].to_i
|
49
53
|
y = dot[:y].to_i
|
50
54
|
pix = image.pixel_color(x, y)
|
51
|
-
l =
|
55
|
+
l = magick_pixel(pix, x, y, set_not_detected_light)
|
52
56
|
points += 1 if l
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
dark = true if points >= (dots.length/100)*percent
|
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
|
60
64
|
if @with_debug
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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 '===================================================================='
|
67
71
|
end
|
68
72
|
dark
|
69
73
|
end
|
70
74
|
|
71
|
-
def self.set_debug_data(show_info=false, draw_file=false)
|
72
|
-
@with_debug=show_info
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
77
81
|
end
|
78
82
|
|
79
|
-
|
83
|
+
def self.draw_debug_files(image, x, y, oldx, oldy)
|
84
|
+
return unless oldx && oldy
|
80
85
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
image.write(@debug_file_path)
|
88
|
-
end
|
89
|
-
end
|
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
|
90
92
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: is_dark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergei Illarionov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rmagick
|
@@ -41,7 +41,8 @@ licenses:
|
|
41
41
|
- MIT
|
42
42
|
metadata:
|
43
43
|
source_code_uri: https://github.com/butteff/is_dark_ruby_gem
|
44
|
-
post_install_message:
|
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
|
45
46
|
rdoc_options: []
|
46
47
|
require_paths:
|
47
48
|
- lib
|
@@ -56,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
57
|
- !ruby/object:Gem::Version
|
57
58
|
version: '0'
|
58
59
|
requirements: []
|
59
|
-
rubygems_version: 3.
|
60
|
+
rubygems_version: 3.4.19
|
60
61
|
signing_key:
|
61
62
|
specification_version: 4
|
62
63
|
summary: Detects a dark background under an area or by a color code
|