redgreenblue 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3acd4edad3c6c358886e6c49138133f207571b610d8ea980fd6ff4d0cb38b7a8
4
- data.tar.gz: 81833b3ed57dfcde3c0558f196e7be284ac040e56330019a994e214b39c75463
3
+ metadata.gz: bb9542466d29c9ddcec8d53f6a374c228cc39f22b8d17e4c7c5b93f4f6095821
4
+ data.tar.gz: 53b0e22ef0143e5110fc33d6f0cd68615a5b9931c1cd31a790dfc08adc9f2198
5
5
  SHA512:
6
- metadata.gz: 57a8b63684a52f0495ee14c47a391a42bce9ce40914b9eee7523d1685fc4a66135ccb453b9c5c2ab68900dc700266e71651cb420fd0cd3cbdcf57cecb3012d27
7
- data.tar.gz: 78917822fe1a7cd67acc98912adf1107736609fd996300003bc7c77ff02cbfcdabf6efce4b86481f27dbb568612237dbee726563de76dab1b60188b9c27e2ef8
6
+ metadata.gz: dd97d2edb6a6503c99aa1ee055297d2dcfa21a330234b3c6fb01cf2551cad6ca5c7796103c94d99a7ba51ee33547f508551e3e4376029243eccbd77199b5ddde
7
+ data.tar.gz: 3b7e6ca19d6c3ee4aafda566cd4187c735e4af0d59f2a2ee09f7e7602a7efb2359c8adaed08eb14c2af9e36c254e299bcbb253cea8755b5b8ead7f8dc7816541
@@ -10,8 +10,11 @@ require 'redgreenblue/48bit'
10
10
  require 'redgreenblue/hex'
11
11
  require 'redgreenblue/int'
12
12
 
13
+ require 'redgreenblue/hsl'
14
+ require 'redgreenblue/hsv'
15
+ require 'redgreenblue/hsb'
16
+
13
17
  require 'redgreenblue/gamma'
14
- require 'redgreenblue/hsl_hsv'
15
18
  require 'redgreenblue/cie'
16
19
 
17
20
  require 'redgreenblue/inspect'
@@ -0,0 +1,39 @@
1
+ require 'redgreenblue/hsv'
2
+
3
+ class RGB
4
+
5
+ ########################################################################
6
+ # Class methods #
7
+ ########################################################################
8
+
9
+ class << self
10
+
11
+ alias hsb hsv
12
+
13
+ end
14
+
15
+ ########################################################################
16
+ # Instance methods #
17
+ ########################################################################
18
+
19
+ alias hsb hsv
20
+
21
+ alias hsb_h hsv_h
22
+
23
+ alias hsb_s hsv_s
24
+
25
+ alias hsb_b hsv_v
26
+
27
+ alias hsb= hsv=
28
+
29
+ alias hsb_h= hsv_h=
30
+
31
+ alias hsb_s= hsv_s=
32
+
33
+ alias hsb_b= hsv_v=
34
+
35
+ alias hsb_rotate! hsv_rotate!
36
+
37
+ alias hsb_rotate hsv_rotate
38
+
39
+ end
@@ -0,0 +1,96 @@
1
+ require 'redgreenblue/hsx_shared'
2
+ require 'redgreenblue/math'
3
+
4
+ class RGB
5
+
6
+ ########################################################################
7
+ # Class methods #
8
+ ########################################################################
9
+
10
+ class << self
11
+
12
+ # Creates a new RGB object from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
13
+ def hsl(*a)
14
+ new hsl_to_values(*a)
15
+ end
16
+
17
+ # Calculates RGB values from HSL.
18
+ # Given hue (0..360), saturation (0..1), and lightness (0..1),
19
+ # returns red, green, and blue as three values between 0 and 1.
20
+ def hsl_to_values(*a)
21
+ hsm_to_values(:hsl, a)
22
+ end
23
+
24
+ end
25
+
26
+ ########################################################################
27
+ # Instance methods #
28
+ ########################################################################
29
+
30
+ # Returns color as HSL:
31
+ # hue (0..360), saturation (0..1), lightness (0..1).
32
+ # When saturation is 0, hue is nil.
33
+ def hsl
34
+ hsl_hsv_c[0]
35
+ end
36
+
37
+ # Returns the object's HSL-hue (0..360).
38
+ def hsl_h
39
+ hsl[0]
40
+ end
41
+
42
+ # Returns the object's HSL-saturation (0..1).
43
+ def hsl_s
44
+ hsl[1]
45
+ end
46
+
47
+ # Returns the object's HSL-lightness (0..1).
48
+ def hsl_l
49
+ hsl[2]
50
+ end
51
+
52
+ # Sets red, green, and blue using HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
53
+ def hsl=(*a)
54
+ self.values = RGB.hsl_to_values(*a)
55
+ end
56
+
57
+ # Sets HSL-hue to a number of degrees (0..360) or nil.
58
+ #
59
+ # Adjusts red, green, and blue, leaving saturation and lightness unchanged.
60
+ # When hue is nil, saturation will be 0.
61
+ def hsl_h=(degrees)
62
+ self.hsl = hsl.fill(degrees,0,1)
63
+ end
64
+
65
+ # Sets HSL-saturation to a value between 0 and 1.
66
+ #
67
+ # Adjusts red, green, and blue, leaving hue and lightness unchanged.
68
+ # When saturation is 0, hue will be nil.
69
+ def hsl_s=(value)
70
+ self.hsl = hsl.fill(value ,1,1)
71
+ end
72
+
73
+ # Sets HSL-lightness to a value between 0 and 1.
74
+ #
75
+ # Adjusts red, green, and blue, leaving hue and saturation unchanged.
76
+ # When lightness is 0 or 1, hue will be nil, and saturation will be 0.
77
+ def hsl_l=(value)
78
+ self.hsl = hsl.fill(value ,2,1)
79
+ end
80
+
81
+ # Sets red, green, and blue by rotating the object's HSL-hue a number of degrees.
82
+ def hsl_rotate!(degrees)
83
+ self.hsl = zip_add(hsl, [degrees, 0, 0])
84
+ self
85
+ end
86
+
87
+ # Creates one or more new RGB objects by rotating this object's HSL-hue a number of degrees.
88
+ def hsl_rotate(a_degrees, *b_degrees)
89
+ if a_degrees.class != Array and b_degrees.none?
90
+ RGB.hsl zip_add(hsl, [a_degrees, 0, 0])
91
+ else
92
+ ( [a_degrees] + b_degrees ).flatten.map { |degrees| RGB.hsl zip_add(hsl, [degrees, 0, 0]) }
93
+ end
94
+ end
95
+
96
+ end
@@ -0,0 +1,118 @@
1
+ require 'redgreenblue/hsx_shared'
2
+ require 'redgreenblue/math'
3
+
4
+ class RGB
5
+
6
+ ########################################################################
7
+ # Class methods #
8
+ ########################################################################
9
+
10
+ class << self
11
+
12
+ # Creates a new RGB object from HSV values: hue (0..360), saturation (0..1), and value (0..1).
13
+ #
14
+ # ::hsb is an alias for ::hsv.
15
+ def hsv(*a)
16
+ new hsv_to_values(*a)
17
+ end
18
+
19
+ # Calculates RGB values from HSV.
20
+ # Given hue (0..360), saturation (0..1), and value (0..1),
21
+ # returns red, green, and blue as three values between 0 and 1.
22
+ def hsv_to_values(*a)
23
+ hsm_to_values(:hsv, a)
24
+ end
25
+
26
+ end
27
+
28
+ ########################################################################
29
+ # Instance methods #
30
+ ########################################################################
31
+
32
+ # Returns color as HSV:
33
+ # hue (0..360), saturation (0..1), value (0..1).
34
+ # When saturation is 0, hue is nil.
35
+ #
36
+ # #hsb is an alias for #hsv.
37
+ def hsv
38
+ hsl_hsv_c[1]
39
+ end
40
+
41
+ # Returns the object's HSV-hue (0..360).
42
+ #
43
+ # #hsb_h is an alias for #hsv_h.
44
+ def hsv_h
45
+ hsv[0]
46
+ end
47
+
48
+ # Returns the object's HSV-saturation (0..1).
49
+ #
50
+ # #hsb_s is an alias for #hsv_s.
51
+ def hsv_s
52
+ hsv[1]
53
+ end
54
+
55
+ # Returns the object's HSV-value (0..1).
56
+ #
57
+ # #hsb_b is an alias for #hsv_v.
58
+ def hsv_v
59
+ hsv[2]
60
+ end
61
+
62
+ # Sets red, green, and blue using HSV values: hue (0..360), saturation (0..1), and value (0..1).
63
+ #
64
+ # #hsb= is an alias for #hsv=.
65
+ def hsv=(*a)
66
+ self.values = RGB.hsv_to_values(*a)
67
+ end
68
+
69
+ # Sets HSV-hue to a number of degrees (0..360) or nil.
70
+ #
71
+ # Adjusts red, green, and blue, leaving HSV-saturation and -value unchanged.
72
+ # When hue is nil, saturation will be 0.
73
+ #
74
+ # #hsb_h= is an alias for #hsv_h=.
75
+ def hsv_h=(degrees)
76
+ self.hsv = hsv.fill(degrees,0,1)
77
+ end
78
+
79
+ # Sets HSV-saturation to a number between 0 and 1.
80
+ #
81
+ # Adjusts red, green, and blue, leaving HSV-hue and -value unchanged.
82
+ # When saturation is 0, hue will be nil.
83
+ #
84
+ # #hsb_s= is an alias for #hsv_s=.
85
+ def hsv_s=(value)
86
+ self.hsv = hsv.fill(value ,1,1)
87
+ end
88
+
89
+ # Sets HSV-value to a number between 0 and 1.
90
+ #
91
+ # Adjusts red, green, and blue, leaving HSV-hue and -saturation unchanged.
92
+ # When value is 0, hue will be nil, and saturation will be 0.
93
+ #
94
+ # #hsb_b= is an alias for #hsv_v=.
95
+ def hsv_v=(value)
96
+ self.hsv = hsv.fill(value ,2,1)
97
+ end
98
+
99
+ # Sets red, green, and blue by rotating the object's HSV-hue a number of degrees.
100
+ #
101
+ # #hsb_rotate! is an alias for #hsv_rotate!.
102
+ def hsv_rotate!(degrees)
103
+ self.hsv = zip_add(hsv, [degrees, 0, 0])
104
+ self
105
+ end
106
+
107
+ # Creates one or more new RGB objects by rotating this object's HSV-hue a number of degrees.
108
+ #
109
+ # #hsb_rotate is an alias for #hsv_rotate.
110
+ def hsv_rotate(a_degrees, *b_degrees)
111
+ if a_degrees.class != Array and b_degrees.none?
112
+ RGB.hsv zip_add(hsv, [a_degrees, 0, 0])
113
+ else
114
+ ( [a_degrees] + b_degrees ).flatten.map { |degrees| RGB.hsv zip_add(hsv, [degrees, 0, 0]) }
115
+ end
116
+ end
117
+
118
+ end
@@ -0,0 +1,94 @@
1
+ class RGB
2
+
3
+ ########################################################################
4
+ # Class methods #
5
+ ########################################################################
6
+
7
+ class << self
8
+
9
+ private
10
+
11
+ # Calculates RGB values from HSL or HSV.
12
+ # With help from:
13
+ # - https://en.wikipedia.org/wiki/HSL_and_HSV
14
+ def hsm_to_values(type, *a)
15
+ raise NotImplementedError unless [:hsl, :hsv].include? type
16
+
17
+ hue, saturation, magnitude = a.flatten
18
+
19
+ if ( saturation == 0 ) or ( ! hue )
20
+ return Array.new(3) { magnitude }
21
+ end
22
+
23
+ hue = hue.modulo 360
24
+
25
+ chroma = case type
26
+ when :hsl
27
+ ( 1 - ( 2 * magnitude - 1 ).abs ) * saturation
28
+ when :hsv
29
+ magnitude * saturation
30
+ end
31
+
32
+ values = [
33
+ chroma,
34
+ ( 1 - ( ( hue / 60.0 ).modulo(2) - 1 ).abs ) * chroma,
35
+ 0
36
+ ]
37
+
38
+ values = case type
39
+ when :hsl
40
+ values.map { |v| ( v + magnitude - chroma / 2.0 ).round(9) }
41
+ when :hsv
42
+ values.map { |v| ( v + magnitude - chroma ).round(9) }
43
+ end
44
+
45
+ # order values according to hue sextant
46
+ [ [0,1,2], [1,0,2], [2,0,1], [2,1,0], [1,2,0], [0,2,1] ][hue.div 60].map { |i| values[i]}
47
+ end
48
+
49
+ end
50
+
51
+ ########################################################################
52
+ # Instance methods #
53
+ ########################################################################
54
+
55
+ private
56
+
57
+ # Compute HSL, HSV, and chroma.
58
+ # With help from:
59
+ # - https://en.wikipedia.org/wiki/HSL_and_HSV
60
+ def hsl_hsv_c
61
+ sorted_hash = to_h
62
+ min, max = sorted_hash.values.values_at(2,0)
63
+
64
+ chroma = max - min
65
+
66
+ hue =
67
+ if chroma == 0
68
+ nil
69
+ else
70
+ ( case sorted_hash.keys.first
71
+ when :red
72
+ 60 * ( ( ( green - blue ) / chroma ).modulo 6 )
73
+ when :green
74
+ 60 * ( ( blue - red ) / chroma + 2 )
75
+ when :blue
76
+ 60 * ( ( red - green ) / chroma + 4 )
77
+ end
78
+ ).round(6)
79
+ end
80
+
81
+ lightness = ( min + max ) / 2.0
82
+
83
+ saturation_hsl =
84
+ chroma == 0 ? 0.0 : ( chroma / ( 1 - (2 * lightness - 1).abs ) ).round(9)
85
+
86
+ value = max
87
+
88
+ saturation_hsv =
89
+ value == 0 ? 0.0 : chroma / value
90
+
91
+ [ [hue, saturation_hsl, lightness], [hue, saturation_hsv, value], chroma ]
92
+ end
93
+
94
+ end
@@ -0,0 +1,9 @@
1
+ class RGB
2
+
3
+ private
4
+
5
+ def zip_add(a,b)
6
+ a.zip(b).map { |ab| ( ab[0] || 0 ) + ab[1] }
7
+ end
8
+
9
+ end
@@ -15,16 +15,6 @@ class RGB
15
15
  self.b = ( ( v & 0x001f ) ) << 3
16
16
  end
17
17
 
18
- # Deprecated: this method will be removed in a future version.
19
- # Returns the color in 16-bit RGB565 format as a string of 0's and 1's.
20
- def rgb565_binary
21
- unless defined? @@warned_rgb565_binary_deprecation
22
- warn "Warning: 'rgb565_binary' has been deprecated and will be removed in a future version of redgreenblue."
23
- @@warned_rgb565_binary_deprecation = true
24
- end
25
- rgb565.bytes.reverse.map { |b| "%08b" % b }.join
26
- end
27
-
28
18
  # Creates a new RGB color from 16-bit RGB565 data.
29
19
  def self.rgb565(rgb565_string)
30
20
  c = self.new
@@ -1,7 +1,7 @@
1
1
  class RGB
2
2
 
3
3
  # redgreenblue version.
4
- VERSION = '0.9.0'
4
+ VERSION = '0.10.0'
5
5
 
6
6
  # Returns RGB::VERSION.
7
7
  def self.version
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redgreenblue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lllist.eu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-02 00:00:00.000000000 Z
11
+ date: 2020-04-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -25,10 +25,14 @@ files:
25
25
  - lib/redgreenblue/gamma.rb
26
26
  - lib/redgreenblue/gif.rb
27
27
  - lib/redgreenblue/hex.rb
28
- - lib/redgreenblue/hsl_hsv.rb
28
+ - lib/redgreenblue/hsb.rb
29
+ - lib/redgreenblue/hsl.rb
30
+ - lib/redgreenblue/hsv.rb
31
+ - lib/redgreenblue/hsx_shared.rb
29
32
  - lib/redgreenblue/inspect.rb
30
33
  - lib/redgreenblue/int.rb
31
34
  - lib/redgreenblue/lazy.rb
35
+ - lib/redgreenblue/math.rb
32
36
  - lib/redgreenblue/misc.rb
33
37
  - lib/redgreenblue/mix.rb
34
38
  - lib/redgreenblue/opt/philipshue.rb
@@ -43,7 +47,9 @@ homepage: https://github.com/lllisteu/redgreenblue
43
47
  licenses:
44
48
  - MIT
45
49
  metadata:
50
+ homepage_uri: https://github.com/lllisteu/redgreenblue
46
51
  changelog_uri: https://github.com/lllisteu/redgreenblue/blob/master/History.md
52
+ documentation_uri: https://www.rubydoc.info/gems/redgreenblue/RGB
47
53
  post_install_message:
48
54
  rdoc_options: []
49
55
  require_paths:
@@ -1,59 +0,0 @@
1
- class RGB
2
-
3
- # Returns color as HSL:
4
- # hue (0..360), saturation (0..1), lightness (0..1).
5
- # When saturation is 0, hue is nil.
6
- def hsl
7
- hsl_hsv_c[0]
8
- end
9
-
10
- # Returns color as HSV:
11
- # hue (0..360), saturation (0..1), value (0..1).
12
- # When saturation is 0, hue is nil.
13
- #
14
- # #hsb is an alias for #hsv.
15
- def hsv
16
- hsl_hsv_c[1]
17
- end
18
-
19
- alias hsb hsv
20
-
21
- private
22
-
23
- # Compute HSL, HSV, and chroma.
24
- # With help from:
25
- # - https://en.wikipedia.org/wiki/HSL_and_HSV
26
- def hsl_hsv_c
27
- sorted_hash = to_h
28
- min, max = sorted_hash.values.values_at(2,0)
29
-
30
- chroma = max - min
31
-
32
- hue =
33
- if chroma == 0
34
- nil
35
- else
36
- case sorted_hash.keys.first
37
- when :red
38
- 60 * ( ( ( green - blue ) / chroma ).modulo 6 )
39
- when :green
40
- 60 * ( ( blue - red ) / chroma + 2 )
41
- when :blue
42
- 60 * ( ( red - green ) / chroma + 4 )
43
- end
44
- end
45
-
46
- lightness = ( min + max ) / 2.0
47
-
48
- saturation_hsl =
49
- chroma == 0 ? 0.0 : ( chroma / ( 1 - (2 * lightness - 1).abs ) ).round(9)
50
-
51
- value = max
52
-
53
- saturation_hsv =
54
- value == 0 ? 0.0 : chroma / value
55
-
56
- [ [hue, saturation_hsl, lightness], [hue, saturation_hsv, value], chroma ]
57
- end
58
-
59
- end