unmagic-color 0.2.0 → 0.2.2
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/CHANGELOG.md +13 -0
- data/README.md +14 -25
- data/lib/unmagic/color/console/help.rb +10 -28
- data/lib/unmagic/color/gradient/bitmap.rb +21 -0
- data/lib/unmagic/color/hsl.rb +9 -0
- data/lib/unmagic/color/oklch.rb +9 -0
- data/lib/unmagic/color/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 971c6c19affe985327415a3993e1b47b739a972a51617a39c92be5c4a1af2f2e
|
|
4
|
+
data.tar.gz: a44de67386e8360347f526b97fc14e925178c475cd93c46fbd831444fd4bc776
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1c24ada886792f4443852806fbe7fbae4a491d7428f83eb61e830ce7b768daf7a0b4d46ec615bd8e4f88b797501baedeafd9b6ada46fc6767d4dc3922e481ebb
|
|
7
|
+
data.tar.gz: fa0b066a29064c5d0db5db70a60489a2201eb5970860865b86b5927eb9f2f64a96e258dcfb85aecf37a0d8e1afda94e702a174e3310fb51408f41500bcad326e
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.2.2] - 2026-01-05
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `Gradient::Bitmap#to_ansi` method to render gradients as colored blocks in terminal
|
|
14
|
+
|
|
15
|
+
## [0.2.1] - 2026-01-05
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
- `to_hex` method for HSL colors (converts via RGB)
|
|
19
|
+
- `to_hex` method for OKLCH colors (converts via RGB)
|
|
20
|
+
|
|
10
21
|
## [0.2.0] - 2026-01-05
|
|
11
22
|
|
|
12
23
|
### Added
|
|
@@ -75,5 +86,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
75
86
|
- HSL color progressions for palette generation
|
|
76
87
|
- Multiple hash functions for string-to-color derivation
|
|
77
88
|
|
|
89
|
+
[0.2.2]: https://github.com/unreasonable-magic/unmagic-color/releases/tag/v0.2.2
|
|
90
|
+
[0.2.1]: https://github.com/unreasonable-magic/unmagic-color/releases/tag/v0.2.1
|
|
78
91
|
[0.2.0]: https://github.com/unreasonable-magic/unmagic-color/releases/tag/v0.2.0
|
|
79
92
|
[0.1.0]: https://github.com/unreasonable-magic/unmagic-color/releases/tag/v0.1.0
|
data/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
A comprehensive Ruby color manipulation library with support for RGB, HSL, and OKLCH color spaces. Parse, convert, and manipulate colors with an intuitive API.
|
|
6
6
|
|
|
7
|
+
We have a fancy-pants WASM based demo here: https://unreasonable-magic.github.io/unmagic-color/
|
|
8
|
+
|
|
7
9
|
## Installation
|
|
8
10
|
|
|
9
11
|
Add this line to your application's Gemfile:
|
|
@@ -159,31 +161,22 @@ Generate color palettes based on color theory relationships:
|
|
|
159
161
|
color = Unmagic::Color.parse("#FF5733")
|
|
160
162
|
|
|
161
163
|
# Complementary - opposite on the color wheel (180°)
|
|
162
|
-
|
|
163
|
-
# => Single color opposite on the wheel
|
|
164
|
+
color.complementary.to_hex # => "#33daff"
|
|
164
165
|
|
|
165
166
|
# Analogous - adjacent colors (default ±30°)
|
|
166
|
-
analogous
|
|
167
|
-
# => [color at -30°, color at +30°]
|
|
168
|
-
analogous = color.analogous(angle: 15) # Custom angle
|
|
167
|
+
color.analogous.map(&:to_hex) # => ["#ff3374", "#ffbe33"]
|
|
169
168
|
|
|
170
169
|
# Triadic - three colors equally spaced (120° apart)
|
|
171
|
-
triadic
|
|
172
|
-
# => [color at +120°, color at +240°]
|
|
170
|
+
color.triadic.map(&:to_hex) # => ["#33ff58", "#5833ff"]
|
|
173
171
|
|
|
174
172
|
# Split Complementary - complement's neighbors (default 180° ±30°)
|
|
175
|
-
|
|
176
|
-
# => [color at 150°, color at 210°]
|
|
177
|
-
split = color.split_complementary(angle: 45) # Custom split
|
|
173
|
+
color.split_complementary.map(&:to_hex) # => ["#33ffbe", "#3374ff"]
|
|
178
174
|
|
|
179
175
|
# Tetradic Square - four colors equally spaced (90° apart)
|
|
180
|
-
|
|
181
|
-
# => [color at +90°, color at +180°, color at +270°]
|
|
176
|
+
color.tetradic_square.map(&:to_hex) # => ["#74ff33", "#33daff", "#be33ff"]
|
|
182
177
|
|
|
183
178
|
# Tetradic Rectangle - two complementary pairs (default 60°)
|
|
184
|
-
|
|
185
|
-
# => [color at +60°, color at +180°, color at +240°]
|
|
186
|
-
tetradic = color.tetradic_rectangle(angle: 30) # Custom angle
|
|
179
|
+
color.tetradic_rectangle.map(&:to_hex) # => ["#daff33", "#33daff", "#5833ff"]
|
|
187
180
|
```
|
|
188
181
|
|
|
189
182
|
### Color Variations
|
|
@@ -196,8 +189,7 @@ Generate shades, tints, tones, and monochromatic palettes:
|
|
|
196
189
|
color = Unmagic::Color.parse("#3366CC")
|
|
197
190
|
|
|
198
191
|
# Monochromatic - same hue with varying lightness
|
|
199
|
-
|
|
200
|
-
# => Array of 5 colors from dark to light
|
|
192
|
+
color.monochromatic(steps: 5).map(&:to_hex) # => ["#0f1f3d", "#214285", "#3366cc", "#7a9cde", "#c2d1f0"]
|
|
201
193
|
|
|
202
194
|
# Shades - progressively darker (mixed with black)
|
|
203
195
|
shades = color.shades(steps: 5)
|
|
@@ -216,11 +208,11 @@ All harmony and variation methods preserve the original color space:
|
|
|
216
208
|
|
|
217
209
|
```ruby
|
|
218
210
|
hsl = Unmagic::Color::HSL.new(hue: 200, saturation: 80, lightness: 50)
|
|
219
|
-
hsl.complementary # =>
|
|
220
|
-
hsl.shades(steps: 3)
|
|
211
|
+
hsl.complementary.to_hex # => "#e65e19"
|
|
212
|
+
hsl.shades(steps: 3).map(&:to_hex) # => ["#1587bf", "#116c99", "#0d5173"]
|
|
221
213
|
|
|
222
214
|
rgb = Unmagic::Color.parse("#FF5733")
|
|
223
|
-
rgb.analogous
|
|
215
|
+
rgb.analogous.map(&:to_hex) # => ["#ff3374", "#ffbe33"]
|
|
224
216
|
```
|
|
225
217
|
|
|
226
218
|
### HSL-Specific Features
|
|
@@ -234,11 +226,8 @@ muted = hsl.desaturate(0.3)
|
|
|
234
226
|
shifted = hsl.adjust_hue(30)
|
|
235
227
|
|
|
236
228
|
# Create color progressions
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
lightness: [30, 50, 70, 85, 95]
|
|
240
|
-
)
|
|
241
|
-
# => Array of 5 HSL colors with varying lightness
|
|
229
|
+
hsl.progression(steps: 5, lightness: [30, 50, 70, 85, 95]).map(&:to_hex)
|
|
230
|
+
# => ["#175e82", "#269dd9", "#7dc4e8", "#bee2f4", "#e9f5fb"]
|
|
242
231
|
```
|
|
243
232
|
|
|
244
233
|
### Generating Colors from Strings
|
|
@@ -14,41 +14,23 @@ module Unmagic
|
|
|
14
14
|
#
|
|
15
15
|
# @return [String] The formatted help text
|
|
16
16
|
def render
|
|
17
|
-
link = highlighter.link("https://github.com/
|
|
17
|
+
link = highlighter.link("https://github.com/unreasonable-magic/unmagic-color")
|
|
18
18
|
|
|
19
19
|
code = highlighter.highlight(<<~RUBY)
|
|
20
|
-
# Parse
|
|
20
|
+
# Parse a color (hex, rgb, hsl, oklch, ansi, css named, x11)
|
|
21
21
|
parse("#ff5733")
|
|
22
|
-
|
|
22
|
+
parse("goldenrod")
|
|
23
|
+
|
|
24
|
+
# Manually create colors
|
|
25
|
+
rgb(255, 87, 51, alpha: percentage(50))
|
|
23
26
|
hsl(9, 100, 60)
|
|
24
27
|
oklch(0.65, 0.22, 30)
|
|
25
|
-
parse("rebeccapurple")
|
|
26
|
-
|
|
27
|
-
# Manipulate colors
|
|
28
|
-
color = parse("#ff5733")
|
|
29
|
-
color.lighten(0.1)
|
|
30
|
-
color.darken(0.1)
|
|
31
|
-
color.saturate(0.1)
|
|
32
|
-
color.desaturate(0.1)
|
|
33
|
-
color.rotate(30)
|
|
34
|
-
|
|
35
|
-
# Convert between formats
|
|
36
|
-
color.to_rgb
|
|
37
|
-
color.to_hsl
|
|
38
|
-
color.to_oklch
|
|
39
|
-
color.to_hex
|
|
40
|
-
color.to_css_oklch
|
|
41
28
|
|
|
42
|
-
#
|
|
43
|
-
|
|
29
|
+
# Show a color card
|
|
30
|
+
show("#ff5733")
|
|
44
31
|
|
|
45
|
-
#
|
|
46
|
-
|
|
47
|
-
hsl(9, 100, 60)
|
|
48
|
-
oklch(0.65, 0.22, 30)
|
|
49
|
-
parse("#ff5733")
|
|
50
|
-
gradient(:linear, ["#FF0000", "#0000FF"])
|
|
51
|
-
percentage(50)
|
|
32
|
+
# Make a rainbow
|
|
33
|
+
puts gradient(:linear, %w[red orange yellow green blue purple], direction: "to right").rasterize(width: 60).to_ansi
|
|
52
34
|
RUBY
|
|
53
35
|
|
|
54
36
|
"#{link}\n\n#{code}"
|
|
@@ -85,6 +85,27 @@ module Unmagic
|
|
|
85
85
|
def to_a
|
|
86
86
|
@pixels.flatten
|
|
87
87
|
end
|
|
88
|
+
|
|
89
|
+
# Convert to ANSI escape codes for terminal display.
|
|
90
|
+
#
|
|
91
|
+
# Renders each pixel as a colored character using 24-bit true color
|
|
92
|
+
# ANSI codes. Each row is joined and rows are separated by newlines.
|
|
93
|
+
#
|
|
94
|
+
# @param fill [String] Character to use for each pixel (default: "█")
|
|
95
|
+
# @return [String] ANSI-colored string representation
|
|
96
|
+
#
|
|
97
|
+
# @example Render a rainbow gradient
|
|
98
|
+
# gradient = Gradient.linear(%w[red yellow green blue])
|
|
99
|
+
# bitmap = gradient.rasterize(width: 40)
|
|
100
|
+
# puts bitmap.to_ansi
|
|
101
|
+
#
|
|
102
|
+
# @example Use custom fill character
|
|
103
|
+
# puts bitmap.to_ansi(fill: "▀")
|
|
104
|
+
def to_ansi(fill: "█")
|
|
105
|
+
@pixels.map do |row|
|
|
106
|
+
row.map { |color| "\e[#{color.to_ansi}m#{fill}\e[0m" }.join
|
|
107
|
+
end.join("\n")
|
|
108
|
+
end
|
|
88
109
|
end
|
|
89
110
|
end
|
|
90
111
|
end
|
data/lib/unmagic/color/hsl.rb
CHANGED
|
@@ -291,6 +291,15 @@ module Unmagic
|
|
|
291
291
|
to_rgb.to_oklch
|
|
292
292
|
end
|
|
293
293
|
|
|
294
|
+
# Convert to hex string.
|
|
295
|
+
#
|
|
296
|
+
# Converts via RGB as an intermediate step.
|
|
297
|
+
#
|
|
298
|
+
# @return [String] The color as a hex string (e.g., "#ff5733")
|
|
299
|
+
def to_hex
|
|
300
|
+
to_rgb.to_hex
|
|
301
|
+
end
|
|
302
|
+
|
|
294
303
|
# Calculate the relative luminance.
|
|
295
304
|
#
|
|
296
305
|
# Converts to RGB first, then calculates luminance.
|
data/lib/unmagic/color/oklch.rb
CHANGED
|
@@ -302,6 +302,15 @@ module Unmagic
|
|
|
302
302
|
Unmagic::Color::RGB.new(red: r, green: g, blue: b, alpha: @alpha)
|
|
303
303
|
end
|
|
304
304
|
|
|
305
|
+
# Convert to hex string.
|
|
306
|
+
#
|
|
307
|
+
# Converts via RGB as an intermediate step.
|
|
308
|
+
#
|
|
309
|
+
# @return [String] The color as a hex string (e.g., "#ff5733")
|
|
310
|
+
def to_hex
|
|
311
|
+
to_rgb.to_hex
|
|
312
|
+
end
|
|
313
|
+
|
|
305
314
|
# Calculate the relative luminance.
|
|
306
315
|
#
|
|
307
316
|
# In OKLCH, the lightness value directly represents perceptual luminance,
|