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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/is_dark.rb +92 -88
  3. metadata +5 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0f9ffb91531bd9d3bcf872aa300bb5037b1d67d6b0919904aad9721fd6b82da
4
- data.tar.gz: d20764491b8cf0c75288b55c24d5252799503f755018d89f65891516b3af69a5
3
+ metadata.gz: a04b8eb2962449d2a8d7ba8060203481e88d6eaaacf07afb2783e0d811f06357
4
+ data.tar.gz: cd9aa4e9690dd089adee3af8acff26337a1b04409f6af39f1d816865251e04bb
5
5
  SHA512:
6
- metadata.gz: 4d24b72d46c7a5bea46c86577ad7268a5764cf68902e140ff19249a2303359f7aacae8d920b3024d707610843ae5efe48c088f8b7e1d4b5cb6fb93c12022a3e4
7
- data.tar.gz: a6e0107e8246faea069085c969fb4af98c658dfb12623df97f49f8ddc1f11d608efe652254fc46a4accfe82d5702d1d19ca853404cece02f30c88c1086fe2215
6
+ metadata.gz: 1fc12a6110c52a8d3f408d57f07e2e8ad3a37e330349353ac8abb39f3b6cc2104187b8724a40161be122dc5b200e938aef179afa64aaad4735a89cd499a34635
7
+ data.tar.gz: 9e1a0d6cbe879f1620c420f0dc12df1d1fe19fa24c7388bbceea7dbf29c8b8eaa0f789cef20904f65528b60398619b75af14de5fbfcdaa397de96b738632d1ad
data/lib/is_dark.rb CHANGED
@@ -1,114 +1,118 @@
1
- require "rmagick"
1
+ # frozen_string_literal: true
2
+
3
+ require 'rmagick'
2
4
  class IsDark
3
- @r = 0
4
- @g = 0
5
- @b = 0
6
- @colorset = 255
7
- @with_debug = false
8
- @with_debug_file = false
9
- @debug_file_path = '/tmp/is_dark_debug_file.pdf'
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
- def self.color(hex)
12
- @r, @g, @b = hex.match(/^#(..)(..)(..)$/).captures.map(&:hex)
13
- @colorset = 255
14
- self.is_dark
15
- end
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
- def self.magick_pixel(pix, x=nil, y=nil, set_not_detected_light=true)
18
- @r, @g, @b = [pix.red.to_f, pix.green.to_f, pix.blue.to_f]
19
- @colorset = 655*255
20
- is = self.is_dark(x, y, set_not_detected_light)
21
- is
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
- def self.magick_pixel_from_blob(x, y, blob, set_not_detected_light=true)
25
- image = Magick::Image.read(blob).first
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 { |xx|
36
- range.each { |yy|
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
- oldx = false
44
- oldy = false
47
+ oldx = false
48
+ oldy = false
45
49
  end
46
50
  p '====================================================================' if @with_debug
47
- dots.each { |dot|
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 = self.magick_pixel(pix, x, y, set_not_detected_light)
55
+ l = magick_pixel(pix, x, y, set_not_detected_light)
52
56
  points += 1 if l
53
- if @with_debug_file
54
- self.draw_debug_files(image, x, y, oldx, oldy)
55
- oldy = y
56
- oldx = x
57
- end
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
- p '===================================================================='
62
- p 'Total Points: '+dots.length.to_s+', dark points amount:'+points.to_s
63
- p 'Is "invert to white not detectd pixels" option enabled?:'+set_not_detected_light.to_s
64
- p 'Signature will be inverted if '+percent.to_s+'% of dots will be dark'
65
- p 'Is inverted?: '+dark.to_s
66
- p '===================================================================='
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
- if draw_file != false
74
- @with_debug_file = true
75
- @debug_file_path = draw_file
76
- end
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
- private
83
+ def self.draw_debug_files(image, x, y, oldx, oldy)
84
+ return unless oldx && oldy
80
85
 
81
- def self.draw_debug_files(image, x, y, oldx, oldy)
82
- if oldx && oldy
83
- gc = Magick::Draw.new
84
- gc.line(x, y, oldx, oldy)
85
- gc.stroke('black')
86
- gc.draw(image)
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
- def self.is_dark(x=nil, y=nil, set_not_detected_light=true) #detects a dark color based on luminance W3 standarts ( https://www.w3.org/TR/WCAG20/#relativeluminancedef )
92
- dark = false
93
- inverted = false
94
- pixel = [@r.to_f, @g.to_f, @b.to_f]
95
- 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"
96
- pixel = [65535.0, 65535.0, 65535.0]
97
- inverted = true
98
- end
99
- calculated = []
100
- pixel.each { |color|
101
- color = color/@colorset
102
- color = color/12.92 if color <= 0.03928
103
- color = ((color+0.055)/1.055)**2.4 if color > 0.03928
104
- calculated << color
105
- }
106
- l = (0.2126*calculated[0] + 0.7152*calculated[1] + 0.0722*calculated[2])
107
- dark = true if l <= 0.05
108
- if @with_debug
109
- debug = {'X': x, 'Y': y, 'R': @r, 'G': @g, 'B': @b, 'luminance value': l, 'is_dark': dark, 'inverted to white': inverted }
110
- p debug
111
- end
112
- dark
113
- end
114
- end
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.9
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-04-15 00:00:00.000000000 Z
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.3.15
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