crystal-image
crystal-image
Common foundation types for Crystal's image ecosystem. Provides standard color types, pixel formats, and geometry primitives that image libraries can build upon.
Installation
Add this to your application's shard.yml:
dependencies:
image:
github: crystal-community/crystal-image
Usage
require "image"
# Create images using factory methods
img = Image.rgba(640, 480)
gray_img = Image.gray(320, 240)
# Create colors
red = Image::Color.rgba(255, 0, 0, 255)
gray = Image::Color.gray(128)
# Set and get pixels
img[10, 20] = red
pixel = img[10, 20]
# Sub-image slicing (using ranges)
# Returns a view sharing the same memory (fast)
sub = img[10..50, 20...60]
sub.fill(red) # Fills the sub-region in the original image
# Iterators (Ergonomic pixel loops)
img.each_pixel do |pixel, x, y|
# Read pixel with coordinates
end
# In-place modification
img.map! do |pixel|
pixel # Return new pixel value here
end
# Bulk Operations
img.fill(Image::Color::RGBA8.new(0, 0, 0, 0)) # Clear image
# Geometry operations
rect = Image.rect(0, 0, 100, 100)
point = Image.point(50, 50)
runion = rect.union(Image.rect(50, 50, 150, 150))
Image Types
All image types implement the PixelBuffer(T) interface:
RGBA8- 8-bit RGBA with premultiplied alpha (4 bytes/pixel)NRGBA8- 8-bit RGBA with non-premultiplied alpha (4 bytes/pixel)Gray8- 8-bit grayscale (1 byte/pixel)RGBA16- 16-bit RGBA with premultiplied alpha (8 bytes/pixel)NRGBA16- 16-bit RGBA with non-premultiplied alpha (8 bytes/pixel)Gray16- 16-bit grayscale (2 bytes/pixel)Uniform(T)- Memory-efficient solid color images
Color Types
8-bit Colors
RGBA8- Premultiplied alpha (0 ≤ r,g,b ≤ a ≤ 255)NRGBA8- Non-premultiplied alphaRGB8- No alpha channelGray8- GrayscaleAlpha8- Alpha only
16-bit Colors
RGBA16- Premultiplied alpha (0 ≤ r,g,b ≤ a ≤ 65535)NRGBA16- Non-premultiplied alphaRGB16- No alpha channelGray16- GrayscaleAlpha16- Alpha only
Alternative Color Spaces
YCbCr8- Y'CbCr color space (JPEG/video)NYCbCrA8- Y'CbCr with alphaCMYK8- CMYK for printingHSL- Hue, Saturation, LightnessHSV- Hue, Saturation, ValueLAB- CIELAB perceptually uniform color space
Geometry Types
Point- 2D integer coordinates with arithmetic operationsRectangle- Axis-aligned rectangle with set operations
# Points
p1 = Image.point(10, 20)
p2 = Image.point(5, 15)
p3 = p1 + p2 # Point(15, 35)
# Rectangles
r1 = Image.rect(0, 0, 100, 100)
r2 = Image.rect(50, 50, 150, 150)
r1.width # 100
r1.height # 100
r1.inset(10) # Shrink by 10 pixels
r1.intersect(r2) # Overlapping region
r1.union(r2) # Bounding box
r1.overlaps?(r2) # true
p1.in?(r1) # true if point inside rect
Contributing
- Fork it (https://github.com/crystal-community/crystal-image/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Ali Naqvi - creator and maintainer
Repository
crystal-image
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- about 7 hours ago
- January 16, 2026
License
MIT License
Links
Synced at
Sat, 17 Jan 2026 13:28:35 GMT
Languages