-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoloursearch.rb
More file actions
executable file
·64 lines (53 loc) · 1.63 KB
/
coloursearch.rb
File metadata and controls
executable file
·64 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
# ____ ____
# / ___/ ___|
# | | \___ \
# | |___ ___) |
# \____|____/ ColourSearch
# Mnpn, 2018
# A simple program for finding the amount of pixels with a specific hexadecimal colour value in an image.
# Usage: ./coloursearch #161616 ~/path/to.file
require 'chunky_png'
pixels = []
found = 0
colour = ARGV[0]
path = ARGV[1]
# Make sure that the required arguments have been provided.
# If not, they're nil.
if colour == nil || path == nil
# Let the user know
puts "Arguments were not provided properly. Usage: `%s #161616 ~/path/to.file`." % $0
# Exit the program.
exit(1)
end
# Try to load the image from the provided path.
begin
img = ChunkyPNG::Image.from_file(path)
rescue => e # Something went wrong. Let's tell the user,
puts "%s.\nMake sure the file is a picture and exists, and try again." % e
exit(1) # and exit the program.
end
height = img.dimension.height
width = img.dimension.width
# Loop through for every pixel in the entire image
height.times do |i|
width.times do |j|
currentpixel = [ChunkyPNG::Color.r(img[j,i]), ChunkyPNG::Color.g(img[j,i]), ChunkyPNG::Color.b(img[j,i])]
# Returns pixels as RGB values. ^
pixels.push currentpixel.map {|x| x.to_s(16).rjust(2, '0')}.join.upcase # Add the pixel as Hex to the array.
end
end
# Make the colour usable.
if colour.start_with?("#")
colour = colour[1..-1]
elsif colour.start_with?("0x")
colour = colour[2..-1]
end
# Repeat this for every colour in the array.
pixels.count { |c|
if c == colour # If the pixel matches the input
found = found + 1
end
}
# Print the results!
puts "Found %d matches for #%s in %s." % [found, colour, path]