Skip to content

Commit

Permalink
Merge pull request #17 from mtgrosser/blending
Browse files Browse the repository at this point in the history
Blending
  • Loading branch information
mtgrosser authored Mar 29, 2022
2 parents 118e4dc + c208557 commit fefe7fb
Show file tree
Hide file tree
Showing 15 changed files with 562 additions and 42 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Rszr 1.3.0 (unreleased)

* Alpha channel control
* Background initialization
* Image blending / watermarking
* Rectangle and image fills
* Color gradients
* Hex color codes always prefixed by "#"

## Rszr 1.2.1 (Mar 22, 2022)

* Fix saving without extension (@mantas)
Expand Down
99 changes: 95 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ image.width => 400
image.height => 300
image.dimensions => [400, 300]
image.format => "jpeg"
image.alpha? => false
image[0, 0] => <Rszr::Color::RGBA @red=38, @green=115, @blue=141, @alpha=255>
image[0, 0].to_hex => "26738dff"
image[0, 0].to_hex(rgb: true) => "26738d"
image[0, 0].to_hex => "#26738dff"
image[0, 0].to_hex(alpha: false) => "#26738d"
```

### Transformations
Expand Down Expand Up @@ -124,6 +125,97 @@ image.flop
image.dup
```

### Image generation

```ruby
# generate new image with transparent background
image = Rszr::Image.new(500, 500, alpha: true, background: Rszr::Color::Transparent)

# fill image with 50% opacity
image.fill!(Rszr::Color::RGBA.new(0, 206, 209, 50))

# define a color gradient
gradient = Rszr::Color::Gradient.new do |g|
g.point 0, 255, 250, 205, 50
g.point 0.5, 135, 206, 250
g.point 1, Rszr::Color::White
end

# draw a rectangle and fill it using the gradient with 45°
image.rectangle!(gradient.to_fill(45), 100, 100, 300, 300)
```

### Colors

```ruby
# pre-defined colors
Rszr::Color::White
Rszr::Color::Black
Rszr::Color::Transparent

# RGB
color = Rszr::Color.rgba(255, 250, 50)
color.red => 255
color.green => 250
color.blue => 50
color.alpha => 255
color.cyan => 0
color.magenta => 5
color.yellow => 205

# RGBA
Rszr::Color.rgba(255, 250, 50, 255)

# CMY
Rszr::Color.cmya(0, 5, 205)

# CMYA
Rszr::Color.cmya(0, 5, 205, 255)
```

### Color gradients

```ruby
# three-color linear gradient with changing opacity
gradient = Rszr::Color::Gradient.new do |g|
g.point 0, 255, 250, 205, 50
g.point 0.5, 135, 206, 250
g.point 1, Rszr::Color::White
end

# alternative syntax
gradient = Rszr::Color::Gradient.new(0 => "#fffacd32", 0.5 => "#87cefa", 1 => "#fff")

# generate fill with 45° angle
fill = gradient.to_fill(45)

# use as image background
image = Rszr::Image.new(500, 500, background: fill)
```

### Watermarking and image blending

```ruby
# load logo
logo = Rszr::Image.load('logo.png')

# load image
image = Rszr::Image.load('image.jpg')

# enable alpha channel
image.alpha = true

# blend it onto the image at position (10, 10)
image.blend!(logo, 10, 10)

# blending modes:
# - copy (default)
# - add
# - subtract
# - reshade
image.blend(logo, 10, 10, mode: :subtract)
```

### Filters

Filters also support bang! and non-bang methods.
Expand Down Expand Up @@ -173,8 +265,7 @@ In order to save interlaced PNGs and progressive JPEGs, set the `interlace` opti
image.save('interlaced.png', interlace: true)
```

As of v1.8.0, `imlib2` doesn't support saving progressive JPEG images yet,
but a [patch](https://git.enlightenment.org/legacy/imlib2.git/commit/?id=37e8c9578897259211284d3590cc38b7f6a718dc) has been submitted.
Saving progressive JPEG images requires `imlib2` >= 1.8.1.

For EL8, there are pre-built RPMs provided by the [onrooby repo](http://downloads.onrooby.com/repo/el/8/x86_64/).

Expand Down
4 changes: 3 additions & 1 deletion ext/rszr/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "errors.h"

VALUE eRszrError = Qnil;
VALUE eRszrInternalError = Qnil;
VALUE eRszrFileNotFound = Qnil;
VALUE eRszrTransformationError = Qnil;
VALUE eRszrErrorWithMessage = Qnil;
Expand Down Expand Up @@ -33,11 +34,12 @@ const int RSZR_MAX_ERROR_INDEX = 13;
void Init_rszr_errors()
{
eRszrError = rb_define_class_under(mRszr, "Error", rb_eStandardError);
eRszrInternalError = rb_define_class_under(mRszr, "InternalError", eRszrError);
eRszrFileNotFound = rb_define_class_under(mRszr, "FileNotFound", eRszrError);
eRszrTransformationError = rb_define_class_under(mRszr, "TransformationError", eRszrError);
eRszrErrorWithMessage = rb_define_class_under(mRszr, "ErrorWithMessage", eRszrError);
eRszrLoadError = rb_define_class_under(mRszr, "LoadError", eRszrErrorWithMessage);
eRszrSaveError = rb_define_class_under(mRszr, "SaveError", eRszrErrorWithMessage);
eRszrSaveError = rb_define_class_under(mRszr, "SaveError", eRszrErrorWithMessage);
}

static void rszr_raise_error_with_message(VALUE rb_error_class, Imlib_Load_Error error)
Expand Down
Loading

0 comments on commit fefe7fb

Please sign in to comment.