mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: a06e8058488c43fa8048b4ffd480d8424d88cc27 Former-commit-id: f0edb842766e8c2d2276625aa23b79ca1739918e Former-commit-id: c55ff7a504d21ce4941fe51596c1beb621ef2082pull/17/head
6 changed files with 137 additions and 52 deletions
@ -0,0 +1,56 @@ |
|||||
|
// <copyright file="Alpha.cs" company="James South">
|
||||
|
// Copyright © James South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageProcessor.Filters |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// An <see cref="IImageProcessor"/> to change the Alpha of an <see cref="Image"/>.
|
||||
|
/// </summary>
|
||||
|
public class Alpha : ParallelImageProcessor |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Alpha"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="percent">The percentage to adjust the opacity of the image. Must be between 0 and 100.</param>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// <paramref name="percent"/> is less than 0 or is greater than 100.
|
||||
|
/// </exception>
|
||||
|
public Alpha(int percent) |
||||
|
{ |
||||
|
Guard.MustBeBetweenOrEqualTo(percent, 0, 100, nameof(percent)); |
||||
|
this.Value = percent; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the alpha value.
|
||||
|
/// </summary>
|
||||
|
public int Value { get; } |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) |
||||
|
{ |
||||
|
double alpha = this.Value / 100.0; |
||||
|
int sourceY = sourceRectangle.Y; |
||||
|
int sourceBottom = sourceRectangle.Bottom; |
||||
|
int startX = sourceRectangle.X; |
||||
|
int endX = sourceRectangle.Right; |
||||
|
|
||||
|
for (int y = startY; y < endY; y++) |
||||
|
{ |
||||
|
if (y >= sourceY && y < sourceBottom) |
||||
|
{ |
||||
|
for (int x = startX; x < endX; x++) |
||||
|
{ |
||||
|
Bgra color = source[x, y]; |
||||
|
double a = color.A * alpha; |
||||
|
target[x, y] = new Bgra(color.B, color.G, color.R, a.ToByte()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue