colorama 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.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +18 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/colorama.gemspec +28 -0
- data/lib/colorama.rb +19 -0
- data/lib/colorama/color.rb +166 -0
- data/lib/colorama/color_extractor.rb +127 -0
- data/lib/colorama/version.rb +5 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ed0bc3f3e2f872b94b7e68779b0b55e09fa0d09ed9f671cb388883047f7d3e13
|
4
|
+
data.tar.gz: 26e6063233fa8a1b9a7384b17b12ea6b7fcf6ace73652fccd96dc26b940dcc2c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a2281dab4194bf96a67e3603d00927aa3ad90b80c70e536d7eb8558941dbe0093f1c159b13eca8f9acf6b064d200543465a117d25c4576aa57fdb1cc05b2717
|
7
|
+
data.tar.gz: 1cac70a44e72aaa9dfe28f9e4cba6132c7d2905193bc006e85159fe2d1076a6f565314d98d53970ed53d41fc9b46d09e7089d18ec028bf3a068c55187617242f
|
@@ -0,0 +1,18 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push,pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 2.7.2
|
14
|
+
- name: Run the default task
|
15
|
+
run: |
|
16
|
+
gem install bundler -v 2.2.14
|
17
|
+
bundle install
|
18
|
+
bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at gustavodiel@hotmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
colorama (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
parallel (1.20.1)
|
12
|
+
parser (3.0.0.0)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
rainbow (3.0.0)
|
15
|
+
rake (13.0.3)
|
16
|
+
regexp_parser (2.1.1)
|
17
|
+
rexml (3.2.4)
|
18
|
+
rmagick (4.2.2)
|
19
|
+
rspec (3.10.0)
|
20
|
+
rspec-core (~> 3.10.0)
|
21
|
+
rspec-expectations (~> 3.10.0)
|
22
|
+
rspec-mocks (~> 3.10.0)
|
23
|
+
rspec-core (3.10.1)
|
24
|
+
rspec-support (~> 3.10.0)
|
25
|
+
rspec-expectations (3.10.1)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.10.0)
|
28
|
+
rspec-mocks (3.10.2)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.10.0)
|
31
|
+
rspec-support (3.10.2)
|
32
|
+
rubocop (1.12.0)
|
33
|
+
parallel (~> 1.10)
|
34
|
+
parser (>= 3.0.0.0)
|
35
|
+
rainbow (>= 2.2.2, < 4.0)
|
36
|
+
regexp_parser (>= 1.8, < 3.0)
|
37
|
+
rexml
|
38
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
39
|
+
ruby-progressbar (~> 1.7)
|
40
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
41
|
+
rubocop-ast (1.4.1)
|
42
|
+
parser (>= 2.7.1.5)
|
43
|
+
ruby-progressbar (1.11.0)
|
44
|
+
unicode-display_width (2.0.0)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
x86_64-linux
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
colorama!
|
51
|
+
rake (~> 13.0)
|
52
|
+
rmagick
|
53
|
+
rspec (~> 3.0)
|
54
|
+
rubocop (~> 1.7)
|
55
|
+
|
56
|
+
BUNDLED WITH
|
57
|
+
2.2.13
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Gustavo Diel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Colorama
|
2
|
+
|
3
|
+
Colorama is a Gem for extracting the most dominant and prominent colors from an image.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
This Gem uses `RMagick`, so the ImageMagick lib must be installed.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'colorama'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install colorama
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
The simplest usage is extrating the information from the image's file directly:
|
28
|
+
```ruby
|
29
|
+
colors = Colorama.extract_from_file('filename')
|
30
|
+
```
|
31
|
+
|
32
|
+
and it returns a `Hash` containg the keys `background`, `primary`, `secondary` and `detail`.
|
33
|
+
|
34
|
+
---
|
35
|
+
|
36
|
+
You may also pass the level of compression to apply to the image, like so:
|
37
|
+
```ruby
|
38
|
+
colors = Colorama.extract_from_file('filename', detail: :high)
|
39
|
+
```
|
40
|
+
|
41
|
+
It accepts `lowest`, `low`, `high` and `highest`
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
46
|
+
|
47
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/colorama. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/colorama/blob/master/CODE_OF_CONDUCT.md).
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
56
|
+
|
57
|
+
## Code of Conduct
|
58
|
+
|
59
|
+
Everyone interacting in the Colorama project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/colorama/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "colorama"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/colorama.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/colorama/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "colorama"
|
7
|
+
spec.version = Colorama::VERSION
|
8
|
+
spec.authors = ["Gustavo Diel"]
|
9
|
+
spec.email = ["gustavodiel@hotmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A Gem that extracts some useful colors from an image"
|
12
|
+
spec.homepage = "https://github.com/gustavodiel/colorama"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
15
|
+
|
16
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/gustavodiel/colorama"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/gustavodiel/colorama/blob/master/CHANGELOG.md"
|
21
|
+
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
end
|
data/lib/colorama.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rmagick'
|
4
|
+
|
5
|
+
require_relative "colorama/version"
|
6
|
+
require_relative "colorama/color"
|
7
|
+
require_relative "colorama/color_extractor"
|
8
|
+
|
9
|
+
module Colorama
|
10
|
+
QUALITIES = %i[lowest low high highest]
|
11
|
+
|
12
|
+
def self.extract_from_file(image_file, quality: :highest)
|
13
|
+
raise ArgumentError("quality must be: #{QUALITIES}") unless QUALITIES.include?(quality)
|
14
|
+
|
15
|
+
image = ::Magick::Image.read(image_file).first
|
16
|
+
|
17
|
+
Colorama::ColorExtractor.most_used_colors(image, quality)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Colorama
|
4
|
+
class Color
|
5
|
+
attr_reader :red, :green, :blue
|
6
|
+
|
7
|
+
WHITE_THRESHOLD = 232
|
8
|
+
BLACK_THRESHOLD = 23
|
9
|
+
|
10
|
+
def self.new_from_rgb(r, g, b)
|
11
|
+
new(r, g, b)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.new_from_double(number)
|
15
|
+
r = ((number / 1_000_000).floor % 1_000_000)
|
16
|
+
g = ((number / 1_000).floor % 1_000)
|
17
|
+
b = (number % 1_000)
|
18
|
+
|
19
|
+
new(r, g, b)
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(red, green, blue)
|
23
|
+
@red = red
|
24
|
+
@green = green
|
25
|
+
@blue = blue
|
26
|
+
end
|
27
|
+
|
28
|
+
def hex
|
29
|
+
@hex ||= begin
|
30
|
+
red_hex = red.to_s(16)
|
31
|
+
green_hex = green.to_s(16)
|
32
|
+
blue_hex = blue.to_s(16)
|
33
|
+
|
34
|
+
red_hex += red_hex unless red_hex.length == 2
|
35
|
+
green_hex += green_hex unless green_hex.length == 2
|
36
|
+
blue_hex += blue_hex unless blue_hex.length == 2
|
37
|
+
|
38
|
+
"#{red_hex}#{green_hex}#{blue_hex}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def dark_color?
|
43
|
+
luminance < 127.5
|
44
|
+
end
|
45
|
+
|
46
|
+
def distinct?(other_color)
|
47
|
+
(
|
48
|
+
(red - other_color.red).abs > 63.75 ||
|
49
|
+
(green - other_color.green).abs > 63.75 ||
|
50
|
+
(blue - other_color.blue).abs > 63.75
|
51
|
+
) && !(
|
52
|
+
(red - green).abs < 7.65 &&
|
53
|
+
(red - blue).abs < 7.65 &&
|
54
|
+
(other_color.red - other_color.green).abs < 7.65 &&
|
55
|
+
(other_color.red - other_color.blue).abs < 7.65
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
# This algorithm is based on our own vision.
|
60
|
+
# The human eye detects more green then red and yellow,
|
61
|
+
# this is the reason of those numbers
|
62
|
+
def luminance
|
63
|
+
@luminance ||= red * 0.2126 + green * 0.7152 + blue * 0.0722
|
64
|
+
end
|
65
|
+
|
66
|
+
def with(min_saturation)
|
67
|
+
normalized_red = red / 255.0
|
68
|
+
normalized_green = green / 255.0
|
69
|
+
normalized_blue = blue / 255.0
|
70
|
+
|
71
|
+
normalized_max = [normalized_red, normalized_green, normalized_blue].max
|
72
|
+
normalized_min = normalized_max - [normalized_red, normalized_green, normalized_blue].min
|
73
|
+
|
74
|
+
s = normalized_max.zero? ? 0 : (normalized_min / normalized_max)
|
75
|
+
|
76
|
+
return self if min_saturation <= s
|
77
|
+
|
78
|
+
hue = if normalized_min.zero?
|
79
|
+
0
|
80
|
+
elsif normalized_red == normalized_max
|
81
|
+
((normalized_green - normalized_blue) / normalized_min) % 6
|
82
|
+
elsif normalized_green == normalized_max
|
83
|
+
2 + ((normalized_green - normalized_red) / normalized_min)
|
84
|
+
else
|
85
|
+
4 + ((normalized_red - normalized_green) / normalized_min)
|
86
|
+
end
|
87
|
+
|
88
|
+
hue += 6 if hue.negative?
|
89
|
+
|
90
|
+
normalized_min = normalized_max * min_saturation
|
91
|
+
x = normalized_min * (1 - (hue % 2).abs - 1)
|
92
|
+
|
93
|
+
case hue
|
94
|
+
when 0...1
|
95
|
+
new_r = normalized_min
|
96
|
+
new_g = x
|
97
|
+
new_b = 0
|
98
|
+
when 1...2
|
99
|
+
new_r = x
|
100
|
+
new_g = normalized_min
|
101
|
+
new_b = 0
|
102
|
+
when 2...3
|
103
|
+
new_r = 0
|
104
|
+
new_g = normalized_min
|
105
|
+
new_b = x
|
106
|
+
when 3...4
|
107
|
+
new_r = 0
|
108
|
+
new_g = x
|
109
|
+
new_b = normalized_min
|
110
|
+
when 4...5
|
111
|
+
new_r = x
|
112
|
+
new_g = 0
|
113
|
+
new_b = normalized_min
|
114
|
+
when 5...6
|
115
|
+
new_r = normalized_min
|
116
|
+
new_g = 0
|
117
|
+
new_b = x
|
118
|
+
else
|
119
|
+
new_r = 0
|
120
|
+
new_g = 0
|
121
|
+
new_b = 0
|
122
|
+
end
|
123
|
+
|
124
|
+
normalized_max -= normalized_min
|
125
|
+
|
126
|
+
Colorama::Color.new_from_double(
|
127
|
+
(((new_r + normalized_max) * 255).floor * 1_000_000) +
|
128
|
+
(((new_g + normalized_max) * 255).floor * 1_000) +
|
129
|
+
((new_b + normalized_max) * 255).floor
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
133
|
+
def contrasting?(other_color)
|
134
|
+
background_luminance = luminance + 12.75
|
135
|
+
foreground_luminance = other_color.luminance + 12.75
|
136
|
+
|
137
|
+
return ((background_luminance / foreground_luminance) > 1.6) if background_luminance > foreground_luminance
|
138
|
+
|
139
|
+
(foreground_luminance / background_luminance) > 1.6
|
140
|
+
end
|
141
|
+
|
142
|
+
def white?
|
143
|
+
red > WHITE_THRESHOLD && green > WHITE_THRESHOLD && blue > WHITE_THRESHOLD
|
144
|
+
end
|
145
|
+
|
146
|
+
def black?
|
147
|
+
red < BLACK_THRESHOLD && green < BLACK_THRESHOLD && blue < BLACK_THRESHOLD
|
148
|
+
end
|
149
|
+
|
150
|
+
def black_or_white?
|
151
|
+
black? || white?
|
152
|
+
end
|
153
|
+
|
154
|
+
def eql?(other)
|
155
|
+
other_hex = other.is_a?(self.class) ? other.hex : other
|
156
|
+
|
157
|
+
other_hex == hex
|
158
|
+
rescue StandardError
|
159
|
+
false
|
160
|
+
end
|
161
|
+
|
162
|
+
def ==(other)
|
163
|
+
eql?(other)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Colorama::ColorExtractor
|
4
|
+
THRESHOLD_MODIFIER = 0.01
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def most_used_colors(image, quality)
|
8
|
+
resized_image = pre_process_image(image, quality)
|
9
|
+
|
10
|
+
height = resized_image.rows
|
11
|
+
|
12
|
+
threshold = (height * THRESHOLD_MODIFIER).to_i
|
13
|
+
proposed = [-1, -1, -1, -1]
|
14
|
+
|
15
|
+
image_colors = {}
|
16
|
+
|
17
|
+
resized_image.each_pixel do |pixel, _c, _r|
|
18
|
+
next if pixel.alpha < 127
|
19
|
+
|
20
|
+
red = pixel.red & 255
|
21
|
+
green = pixel.green & 255
|
22
|
+
blue = pixel.blue & 255
|
23
|
+
|
24
|
+
index = red.to_i * 1_000_000 + green * 1_000 + blue
|
25
|
+
|
26
|
+
image_colors[index] = 0 unless image_colors.include?(index)
|
27
|
+
|
28
|
+
image_colors[index] += 1
|
29
|
+
end
|
30
|
+
|
31
|
+
image_colors.sort_by { |_key, value| -value }
|
32
|
+
|
33
|
+
sorted_colors = []
|
34
|
+
|
35
|
+
image_colors.each do |key, value|
|
36
|
+
sorted_colors << { color: Colorama::Color.new_from_double(key), count: value } if threshold < value
|
37
|
+
end
|
38
|
+
|
39
|
+
proposed[0] = extract_edge_color(sorted_colors)
|
40
|
+
|
41
|
+
sorted_colors.clear
|
42
|
+
|
43
|
+
first_color_dark = !proposed[0].dark_color?
|
44
|
+
|
45
|
+
image_colors.each do |key, value|
|
46
|
+
color = Colorama::Color.new_from_double(key).with(0.15)
|
47
|
+
|
48
|
+
sorted_colors << { color: color, count: value } if color.dark_color? == first_color_dark
|
49
|
+
end
|
50
|
+
|
51
|
+
sorted_colors.each do |entry|
|
52
|
+
color = entry[:color]
|
53
|
+
|
54
|
+
if proposed[1] == -1
|
55
|
+
proposed[1] = color if color.contrasting?(proposed[0])
|
56
|
+
elsif proposed[2] == -1
|
57
|
+
next if !color.contrasting?(proposed[0]) || !proposed[1].distinct?(color)
|
58
|
+
|
59
|
+
proposed[2] = color
|
60
|
+
elsif proposed[3] == -1
|
61
|
+
next if !color.contrasting?(proposed[0]) || !proposed[2].distinct?(color) || !proposed[1].distinct?(color)
|
62
|
+
|
63
|
+
proposed[3] = color
|
64
|
+
break
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
dark_background = proposed[0].dark_color?
|
69
|
+
|
70
|
+
proposed.count.times.each do |i|
|
71
|
+
proposed[i] = Colorama::Color.new_from_double(dark_background && 255_255_255 || 0) if proposed[i] == -1
|
72
|
+
end
|
73
|
+
|
74
|
+
{
|
75
|
+
background: proposed[0],
|
76
|
+
primary: proposed[1],
|
77
|
+
secondary: proposed[2],
|
78
|
+
detail: proposed[3]
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def pre_process_image(image, quality)
|
85
|
+
resize_size = sanitize_quality(quality)
|
86
|
+
|
87
|
+
resize_size.positive? ? scale_image(image, resize_size, resize_size) : image
|
88
|
+
end
|
89
|
+
|
90
|
+
def extract_edge_color(sorted_colors)
|
91
|
+
proposed_edge_color = { color: Colorama::Color.new_from_double(0), count: 1 }
|
92
|
+
|
93
|
+
proposed_edge_color = sorted_colors.first if sorted_colors.count.positive?
|
94
|
+
|
95
|
+
if proposed_edge_color[:color].black_or_white? && sorted_colors.count > 1
|
96
|
+
(1..sorted_colors.count).each do |i|
|
97
|
+
next_proposed_color = sorted_colors[i]
|
98
|
+
if (next_proposed_color[:count] / proposed_edge_color[:count]) > 0.3
|
99
|
+
unless next_proposed_color[:color].black_or_white?
|
100
|
+
proposed_edge_color = next_proposed_color
|
101
|
+
break
|
102
|
+
end
|
103
|
+
else
|
104
|
+
break
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
proposed_edge_color[:color]
|
110
|
+
end
|
111
|
+
|
112
|
+
def scale_image(image, max_width = 512, max_height = 512)
|
113
|
+
min_width = [image.columns, max_width].min
|
114
|
+
min_height = [image.rows, max_height].min
|
115
|
+
|
116
|
+
image.resize_to_fit(min_width, min_height)
|
117
|
+
end
|
118
|
+
|
119
|
+
def sanitize_quality(quality)
|
120
|
+
{
|
121
|
+
lowest: 50,
|
122
|
+
low: 100,
|
123
|
+
high: 250
|
124
|
+
}[quality] || 0
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: colorama
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gustavo Diel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- gustavodiel@hotmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/main.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- CODE_OF_CONDUCT.md
|
26
|
+
- Gemfile
|
27
|
+
- Gemfile.lock
|
28
|
+
- LICENSE.txt
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- bin/console
|
32
|
+
- bin/setup
|
33
|
+
- colorama.gemspec
|
34
|
+
- lib/colorama.rb
|
35
|
+
- lib/colorama/color.rb
|
36
|
+
- lib/colorama/color_extractor.rb
|
37
|
+
- lib/colorama/version.rb
|
38
|
+
homepage: https://github.com/gustavodiel/colorama
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata:
|
42
|
+
allowed_push_host: https://rubygems.org
|
43
|
+
homepage_uri: https://github.com/gustavodiel/colorama
|
44
|
+
source_code_uri: https://github.com/gustavodiel/colorama
|
45
|
+
changelog_uri: https://github.com/gustavodiel/colorama/blob/master/CHANGELOG.md
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.4.0
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.0.3
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: A Gem that extracts some useful colors from an image
|
65
|
+
test_files: []
|