human_colour 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f61159dfb6700a4655b71b7b0dde3d7dff974453210cdb74281e34bab78b1782
4
- data.tar.gz: 9d65916d7129cb05ac2ead4db9d0e827fb9841ef59fecbfc681d19dffa50e472
3
+ metadata.gz: 47af2da1e39857844b4b6586da5dadb801036478f497a9f112cff41f563c1e70
4
+ data.tar.gz: 8327b50b7b74ce6f111b8d9eac377aca5646d929a718b3fd21084c6514ce6030
5
5
  SHA512:
6
- metadata.gz: a0982688d4fc4046d24a1c50eec07f0c93439bcdf0cbd541802866b62e2b0f4635e996c2aa2c3ee3c69883b7bef45603fbb58acf9a66e8eca4a6adb21a096796
7
- data.tar.gz: fac3b685fa6b7653881e9622705b1fac443bd00ac4725534daba4aee949ff5a7306fa79453db4a5c941c229b78ee90120e3e25c5a592c17d8b7443e001547d53
6
+ metadata.gz: 6e7aaba52e1d446819301ab65bd3fa57a93652a998ac0c81468ef4b000e7e1f784e42bb854dac040d5c1af0bfa64e5e40e1e398bfa6ec39936253c5d6cad9f0d
7
+ data.tar.gz: e9aa06df3f2ca7b42370a0ec7c85297a4cbf050fe6fd4d78c78576ac15045cb1119a53b675f3bd9f48c65dc4b76aa946f112d5ba7afb9fb90882592a7ba2834f
data/.rubocop.yml CHANGED
@@ -28,6 +28,12 @@ Layout/LineContinuationSpacing: # new in 1.31
28
28
  Enabled: true
29
29
  Layout/LineEndStringConcatenationIndentation: # new in 1.18
30
30
  Enabled: true
31
+ Layout/LineLength:
32
+ Exclude:
33
+ - "spec/**/*.rb"
34
+ Layout/HashAlignment:
35
+ Exclude:
36
+ - "spec/**/*.rb"
31
37
  Layout/SpaceBeforeBrackets: # new in 1.7
32
38
  Enabled: true
33
39
  Lint/AmbiguousAssignment: # new in 1.7
@@ -293,5 +299,7 @@ Style/SuperWithArgsParentheses: # new in 1.58
293
299
  Enabled: true
294
300
  Style/SwapValues: # new in 1.1
295
301
  Enabled: true
302
+ Style/SymbolArray:
303
+ Enabled: false
296
304
  Style/YAMLFileRead: # new in 1.53
297
305
  Enabled: true
data/README.md CHANGED
@@ -19,8 +19,17 @@ gem install human_colour
19
19
  ## Usage
20
20
 
21
21
  ```ruby
22
+ require "human_colour"
23
+
22
24
  HumanColour.parse("rgb(128,64,0)")
23
- #> "dark orange"
25
+ #=> "dark orange"
26
+
27
+ # Return a colour based on the provided locale (:de, :en, :es, :fr, :it, :pt)
28
+ HumanColour.parse("rgb(128,64,0)", locale: :es)
29
+ #=> "naranja oscuro"
30
+
31
+ HumanColour.parse("rgb(255, 128, 255", locale: :de)
32
+ #=> "hell lila"
24
33
  ```
25
34
 
26
35
  ![alt text](https://github.com/mconnell/human_colour/blob/main/doc/colours.png?raw=true)
data/doc/colours.png CHANGED
Binary file
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/ModuleLength
4
+ module HumanColour
5
+ LOCALES = {
6
+ en: {
7
+ tone: {
8
+ light: "light",
9
+ dark: "dark"
10
+ },
11
+ colour: {
12
+ red: "red",
13
+ brown: "brown",
14
+ orange: "orange",
15
+ yellow: "yellow",
16
+ green: "green",
17
+ blue: "blue",
18
+ purple: "purple",
19
+ pink: "pink",
20
+ grey: "grey",
21
+ black: "black",
22
+ white: "white"
23
+ }
24
+ },
25
+
26
+ es: {
27
+ tone: {
28
+ light: "claro",
29
+ dark: "oscuro"
30
+ },
31
+ colour: {
32
+ red: "rojo",
33
+ brown: "marrón",
34
+ orange: "naranja",
35
+ yellow: "amarillo",
36
+ green: "verde",
37
+ blue: "azul",
38
+ purple: "morado",
39
+ pink: "rosa",
40
+ grey: "gris",
41
+ black: "negro",
42
+ white: "blanco"
43
+ }
44
+ },
45
+
46
+ it: {
47
+ tone: {
48
+ light: "chiaro",
49
+ dark: "scuro"
50
+ },
51
+ colour: {
52
+ red: "rosso",
53
+ brown: "marrone",
54
+ orange: "arancione",
55
+ yellow: "giallo",
56
+ green: "verde",
57
+ blue: "blu",
58
+ purple: "viola",
59
+ pink: "rosa",
60
+ grey: "grigio",
61
+ black: "nero",
62
+ white: "bianco"
63
+ }
64
+ },
65
+
66
+ fr: {
67
+ tone: {
68
+ light: "clair",
69
+ dark: "foncé"
70
+ },
71
+ colour: {
72
+ red: "rouge",
73
+ brown: "brun",
74
+ orange: "orange",
75
+ yellow: "jaune",
76
+ green: "vert",
77
+ blue: "bleu",
78
+ purple: "violet",
79
+ pink: "rose",
80
+ grey: "gris",
81
+ black: "noir",
82
+ white: "blanc"
83
+ }
84
+ },
85
+
86
+ de: {
87
+ tone: {
88
+ light: "hell",
89
+ dark: "dunkel"
90
+ },
91
+ colour: {
92
+ red: "rot",
93
+ brown: "braun",
94
+ orange: "orange",
95
+ yellow: "gelb",
96
+ green: "grün",
97
+ blue: "blau",
98
+ purple: "lila",
99
+ pink: "rosa",
100
+ grey: "grau",
101
+ black: "schwarz",
102
+ white: "weiß"
103
+ }
104
+ },
105
+
106
+ pt: {
107
+ tone: {
108
+ light: "claro",
109
+ dark: "escuro"
110
+ },
111
+ colour: {
112
+ red: "vermelho",
113
+ brown: "marrom",
114
+ orange: "laranja",
115
+ yellow: "amarelo",
116
+ green: "verde",
117
+ blue: "azul",
118
+ purple: "roxo",
119
+ pink: "rosa",
120
+ grey: "cinza",
121
+ black: "preto",
122
+ white: "branco"
123
+ }
124
+ }
125
+ }.freeze
126
+ end
127
+ # rubocop:enable Metrics/ModuleLength
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanColour
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/human_colour.rb CHANGED
@@ -1,15 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "human_colour/version"
4
+ require_relative "human_colour/locales"
4
5
 
5
6
  module HumanColour
6
7
  class Error < StandardError; end
7
8
 
8
9
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
9
- def self.parse(rgb_string)
10
+ def self.parse(rgb_string, locale: :en)
10
11
  matches = rgb_string.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/i)
11
12
  raise Error if matches.nil?
12
13
 
14
+ return LOCALES[locale][:colour][:white] if rgb_string == "rgb(255,255,255)"
15
+ return LOCALES[locale][:colour][:black] if rgb_string == "rgb(0,0,0)"
16
+
13
17
  red = matches[1].to_i / 255.0
14
18
  green = matches[2].to_i / 255.0
15
19
  blue = matches[3].to_i / 255.0
@@ -18,16 +22,17 @@ module HumanColour
18
22
  d = delta(red: red, green: green, blue: blue)
19
23
  s = saturation(delta: d, lightness: l)
20
24
  h = hue(delta: d, red: red, green: green, blue: blue)
21
- t = tone(lightness: l, hue: h)
22
- c = colour(saturation: s, hue: h)
23
-
24
- name = [t, c].compact.join(" ")
25
+ c = colour(saturation: s, hue: h, lightness: l)
26
+ t = tone(lightness: l, hue: h, base: c)
25
27
 
26
- # alias extremes for nicer names
27
- return "white" if rgb_string == "rgb(255,255,255)"
28
- return "black" if rgb_string == "rgb(0,0,0)"
28
+ localised_tone = LOCALES[locale][:tone][t]
29
+ localised_colour = LOCALES[locale][:colour][c]
29
30
 
30
- name
31
+ if [:en, :de].include?(locale)
32
+ [localised_tone, localised_colour].compact.join(" ")
33
+ else
34
+ [localised_colour, localised_tone].compact.join(" ")
35
+ end
31
36
  end
32
37
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
33
38
 
@@ -65,28 +70,32 @@ module HumanColour
65
70
  end
66
71
  private_class_method :hue
67
72
 
68
- def self.tone(lightness:, hue:)
69
- return nil if (260..320).cover?(hue) && lightness >= 0.22 && lightness < 0.28
70
- return "dark" if lightness < 0.28
71
- return "light" if lightness > 0.74
73
+ def self.tone(lightness:, hue:, base:)
74
+ return nil if (260..320).cover?(hue) && lightness.between?(0.22, 0.28)
75
+ return nil if base == :brown
76
+
77
+ # All other hues
78
+ return :dark if lightness < 0.28
79
+ return :light if lightness > 0.74
72
80
 
73
81
  nil
74
82
  end
75
83
  private_class_method :tone
76
84
 
77
85
  # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
78
- def self.colour(saturation:, hue:)
79
- return "grey" if saturation < 0.15
86
+ def self.colour(saturation:, hue:, lightness:)
87
+ return :grey if saturation < 0.15
88
+ return :brown if (15..50).cover?(hue) && lightness < 0.5
80
89
 
81
90
  case hue
82
- when 0...15, 355..360 then "red"
83
- when 15...45 then "orange"
84
- when 45...70 then "yellow"
85
- when 70...170 then "green"
86
- when 170...250 then "blue"
87
- when 250...320 then "purple"
88
- when 320...355 then "pink"
89
- else "grey"
91
+ when 0...15, 355..360 then :red
92
+ when 15...45 then :orange
93
+ when 45...70 then :yellow
94
+ when 70...170 then :green
95
+ when 170...250 then :blue
96
+ when 250...320 then :purple
97
+ when 320...355 then :pink
98
+ else :grey
90
99
  end
91
100
  end
92
101
  # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_colour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Connell
@@ -22,6 +22,7 @@ files:
22
22
  - Rakefile
23
23
  - doc/colours.png
24
24
  - lib/human_colour.rb
25
+ - lib/human_colour/locales.rb
25
26
  - lib/human_colour/version.rb
26
27
  - sig/human_colour.rbs
27
28
  homepage: https://github.com/mconnell/human_colour