color_converters 0.1.2 → 0.1.4
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 +4 -4
- data/README.md +190 -161
- data/lib/color_converters/base_converter.rb +184 -163
- data/lib/color_converters/color.rb +18 -16
- data/lib/color_converters/converters/cielab_converter.rb +95 -88
- data/lib/color_converters/converters/cielch_converter.rb +61 -52
- data/lib/color_converters/converters/cmyk_converter.rb +59 -56
- data/lib/color_converters/converters/hex_converter.rb +45 -32
- data/lib/color_converters/converters/hsl_converter.rb +68 -64
- data/lib/color_converters/converters/hsl_string_converter.rb +46 -25
- data/lib/color_converters/converters/hsv_converter.rb +60 -58
- data/lib/color_converters/converters/name_converter.rb +67 -183
- data/lib/color_converters/converters/null_converter.rb +19 -17
- data/lib/color_converters/converters/oklab_converter.rb +102 -0
- data/lib/color_converters/converters/oklch_converter.rb +61 -0
- data/lib/color_converters/converters/rgb_converter.rb +109 -29
- data/lib/color_converters/converters/rgb_string_converter.rb +49 -32
- data/lib/color_converters/converters/xyz_converter.rb +110 -127
- data/lib/color_converters/version.rb +1 -1
- data/lib/color_converters.rb +31 -28
- metadata +34 -4
@@ -1,127 +1,110 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
def
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
y = (rr * 0.2126729) + (gg * 0.7151522) + (bb * 0.0721750)
|
112
|
-
z = (rr * 0.0193339) + (gg * 0.1191920) + (bb * 0.9503041)
|
113
|
-
|
114
|
-
# Now, scale X, Y, Z so that Y for D65 white would be 100.
|
115
|
-
x *= 100.0
|
116
|
-
y *= 100.0
|
117
|
-
z *= 100.0
|
118
|
-
|
119
|
-
# Clamping XYZ values to prevent out-of-gamut issues and numerical errors and ensures these values stay within the valid and expected range.
|
120
|
-
x = x.clamp(0.0..95.047)
|
121
|
-
y = y.clamp(0.0..100.0)
|
122
|
-
z = z.clamp(0.0..108.883)
|
123
|
-
|
124
|
-
[x, y, z]
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ColorConverters
|
4
|
+
class XyzConverter < BaseConverter
|
5
|
+
def self.matches?(colour_input)
|
6
|
+
return false unless colour_input.is_a?(Hash)
|
7
|
+
|
8
|
+
colour_input.keys - [:x, :y, :z] == []
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.d65
|
12
|
+
{ x: 95.047, y: 100.0, z: 108.883 }
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.bounds
|
16
|
+
{ x: [0.0, 100.0], y: [0.0, 100.0], z: [0.0, 110.0] }
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def validate_input(colour_input)
|
22
|
+
XyzConverter.bounds.collect do |key, range|
|
23
|
+
"#{key} must be between #{range[0]} and #{range[1]}" unless colour_input[key].to_f.between?(*range)
|
24
|
+
end.compact
|
25
|
+
end
|
26
|
+
|
27
|
+
def input_to_rgba(colour_input)
|
28
|
+
r, g, b = XyzConverter.xyz_to_rgb(colour_input)
|
29
|
+
|
30
|
+
[r, g, b, 1.0]
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.xyz_to_rgb(xyz_hash)
|
34
|
+
# [0, 100]
|
35
|
+
x = xyz_hash[:x].to_d
|
36
|
+
y = xyz_hash[:y].to_d
|
37
|
+
z = xyz_hash[:z].to_d
|
38
|
+
|
39
|
+
# Convert XYZ (typically with Y=100 for white) to normalized XYZ (Y=1 for white).
|
40
|
+
# The transformation matrix expects X, Y, Z values in the 0-1 range.
|
41
|
+
x /= 100.0.to_d
|
42
|
+
y /= 100.0.to_d
|
43
|
+
z /= 100.0.to_d
|
44
|
+
|
45
|
+
# Convert normalized XYZ to Linear sRGB values using sRGB's own white, D65 (no chromatic adaptation)
|
46
|
+
# https://www.w3.org/TR/css-color-4/#color-conversion-code
|
47
|
+
conversion_matrix = ::Matrix[
|
48
|
+
[BigDecimal('3.2409699419045213'), BigDecimal('-1.5373831775700935'), BigDecimal('-0.4986107602930033')],
|
49
|
+
[BigDecimal('-0.9692436362808798'), BigDecimal('1.8759675015077206'), BigDecimal('0.04155505740717561')],
|
50
|
+
[BigDecimal('0.05563007969699361'), BigDecimal('-0.20397695888897657'), BigDecimal('1.0569715142428786')]
|
51
|
+
]
|
52
|
+
|
53
|
+
rr, gg, bb = (conversion_matrix * ::Matrix.column_vector([x, y, z])).to_a.flatten
|
54
|
+
|
55
|
+
# [0, 1]
|
56
|
+
RgbConverter.lrgb_to_rgb([rr, gg, bb])
|
57
|
+
end
|
58
|
+
|
59
|
+
# http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
|
60
|
+
def self.rgb_to_xyz(rgb_array_frac)
|
61
|
+
rr, gg, bb = RgbConverter.rgb_to_lrgb(rgb_array_frac)
|
62
|
+
|
63
|
+
# Convert using the RGB/XYZ matrix and sRGB's own white, D65 (no chromatic adaptation)
|
64
|
+
# https://www.w3.org/TR/css-color-4/#color-conversion-code
|
65
|
+
conversion_matrix = ::Matrix[
|
66
|
+
[BigDecimal('0.4123907992659595'), BigDecimal('0.35758433938387796'), BigDecimal('0.1804807884018343')],
|
67
|
+
[BigDecimal('0.21263900587151036'), BigDecimal('0.7151686787677559'), BigDecimal('0.07219231536073371')],
|
68
|
+
[BigDecimal('0.01933081871559185'), BigDecimal('0.11919477979462599'), BigDecimal('0.9505321522496606')]
|
69
|
+
]
|
70
|
+
|
71
|
+
x, y, z = (conversion_matrix * ::Matrix.column_vector([rr, gg, bb])).to_a.flatten
|
72
|
+
|
73
|
+
# Now, scale X, Y, Z so that Y for D65 white would be 100.
|
74
|
+
x *= 100.0
|
75
|
+
y *= 100.0
|
76
|
+
z *= 100.0
|
77
|
+
|
78
|
+
# Clamping XYZ values to prevent out-of-gamut issues and numerical errors and ensures these values stay within the valid and expected range.
|
79
|
+
# x = x.clamp(0.0..95.047)
|
80
|
+
# y = y.clamp(0.0..100.0)
|
81
|
+
# z = z.clamp(0.0..108.883)
|
82
|
+
|
83
|
+
[x, y, z]
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.d50_to_d65(xyz_array)
|
87
|
+
x, y, z = xyz_array
|
88
|
+
|
89
|
+
conversion_matrix = ::Matrix[
|
90
|
+
[BigDecimal('0.955473421488075'), BigDecimal('-0.02309845494876471'), BigDecimal('0.06325924320057072')],
|
91
|
+
[BigDecimal('-0.0283697093338637'), BigDecimal('1.0099953980813041'), BigDecimal('0.021041441191917323')],
|
92
|
+
[BigDecimal('0.012314014864481998'), BigDecimal('-0.020507649298898964'), BigDecimal('1.330365926242124')]
|
93
|
+
]
|
94
|
+
|
95
|
+
(conversion_matrix * ::Matrix.column_vector([x, y, z])).to_a.flatten
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.d65_to_d50(xyz_array)
|
99
|
+
x, y, z = xyz_array
|
100
|
+
|
101
|
+
conversion_matrix = ::Matrix[
|
102
|
+
[BigDecimal('1.0479297925449969'), BigDecimal('0.022946870601609652'), BigDecimal('-0.05019226628920524')],
|
103
|
+
[BigDecimal('0.02962780877005599'), BigDecimal('0.9904344267538799'), BigDecimal('-0.017073799063418826')],
|
104
|
+
[BigDecimal('-0.009243040646204504'), BigDecimal('0.015055191490298152'), BigDecimal('0.7518742814281371')]
|
105
|
+
]
|
106
|
+
|
107
|
+
(conversion_matrix * ::Matrix.column_vector([x, y, z])).to_a.flatten
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/lib/color_converters.rb
CHANGED
@@ -1,28 +1,31 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'forwardable'
|
4
|
-
|
5
|
-
require 'color_converters/version'
|
6
|
-
|
7
|
-
require 'color_converters/color'
|
8
|
-
require 'color_converters/base_converter'
|
9
|
-
|
10
|
-
|
11
|
-
require 'color_converters/converters/
|
12
|
-
require 'color_converters/converters/
|
13
|
-
require 'color_converters/converters/
|
14
|
-
require 'color_converters/converters/
|
15
|
-
require 'color_converters/converters/
|
16
|
-
require 'color_converters/converters/
|
17
|
-
require 'color_converters/converters/
|
18
|
-
require 'color_converters/converters/
|
19
|
-
require 'color_converters/converters/
|
20
|
-
|
21
|
-
require 'color_converters/converters/
|
22
|
-
require 'color_converters/converters/
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
require 'color_converters/version'
|
6
|
+
|
7
|
+
require 'color_converters/color'
|
8
|
+
require 'color_converters/base_converter'
|
9
|
+
|
10
|
+
# Converters
|
11
|
+
require 'color_converters/converters/rgb_converter'
|
12
|
+
require 'color_converters/converters/rgb_string_converter'
|
13
|
+
require 'color_converters/converters/hex_converter'
|
14
|
+
require 'color_converters/converters/hsl_converter'
|
15
|
+
require 'color_converters/converters/hsl_string_converter'
|
16
|
+
require 'color_converters/converters/hsv_converter'
|
17
|
+
require 'color_converters/converters/cmyk_converter'
|
18
|
+
require 'color_converters/converters/xyz_converter'
|
19
|
+
require 'color_converters/converters/cielab_converter'
|
20
|
+
require 'color_converters/converters/cielch_converter'
|
21
|
+
require 'color_converters/converters/oklab_converter'
|
22
|
+
require 'color_converters/converters/oklch_converter'
|
23
|
+
|
24
|
+
require 'color_converters/converters/name_converter'
|
25
|
+
require 'color_converters/converters/null_converter'
|
26
|
+
|
27
|
+
module ColorConverters
|
28
|
+
class Error < StandardError; end
|
29
|
+
class InvalidColorError < Error; end
|
30
|
+
# Your code goes here...
|
31
|
+
end
|
metadata
CHANGED
@@ -1,28 +1,56 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: color_converters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Louis Davis
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-08-21 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activesupport
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 6.1.3
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 6.1.3
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: color_swatch_collection
|
14
28
|
requirement: !ruby/object:Gem::Requirement
|
15
29
|
requirements:
|
16
30
|
- - "~>"
|
17
31
|
- !ruby/object:Gem::Version
|
18
|
-
version:
|
32
|
+
version: 0.1.0
|
19
33
|
type: :runtime
|
20
34
|
prerelease: false
|
21
35
|
version_requirements: !ruby/object:Gem::Requirement
|
22
36
|
requirements:
|
23
37
|
- - "~>"
|
24
38
|
- !ruby/object:Gem::Version
|
25
|
-
version:
|
39
|
+
version: 0.1.0
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: matrix
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
26
54
|
description: Convert colors to hex/rgb/hsv/cmyk/hsl/xyz/cielab/oklch
|
27
55
|
email:
|
28
56
|
- LouisWilliamDavis@gmail.com
|
@@ -44,6 +72,8 @@ files:
|
|
44
72
|
- lib/color_converters/converters/hsv_converter.rb
|
45
73
|
- lib/color_converters/converters/name_converter.rb
|
46
74
|
- lib/color_converters/converters/null_converter.rb
|
75
|
+
- lib/color_converters/converters/oklab_converter.rb
|
76
|
+
- lib/color_converters/converters/oklch_converter.rb
|
47
77
|
- lib/color_converters/converters/rgb_converter.rb
|
48
78
|
- lib/color_converters/converters/rgb_string_converter.rb
|
49
79
|
- lib/color_converters/converters/xyz_converter.rb
|