Skip to content

Commit 5a7c2cd

Browse files
committed
feat: paint color filter support
1 parent 68a21de commit 5a7c2cd

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

Diff for: src/canvas.android.ts

+7
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ export class DashPathEffect extends ProxyClass<android.graphics.DashPathEffect>
442442
return this;
443443
}
444444
}
445+
export class ColorMatrixColorFilter extends ProxyClass<android.graphics.ColorMatrixColorFilter> {
446+
constructor(values: number[]) {
447+
super();
448+
this.mNative = new android.graphics.ColorMatrixColorFilter(arrayToNativeArray(values, false, false));
449+
return this;
450+
}
451+
}
445452

446453
export class Path extends ProxyClass<com.akylas.canvas.CanvasPath> {
447454
constructor(path?: com.akylas.canvas.CanvasPath) {

Diff for: src/canvas.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ColorParam = Color | number | string;
1717

1818
// export * from './canvas.android'
1919

20+
export type ColorFilter = android.graphics.ColorFilter;
2021
export class Paint {
2122
constructor(paint?: Paint);
2223
color: ColorParam;
@@ -87,6 +88,7 @@ export class Paint {
8788
// public getFontSpacing(): number;
8889
public setXfermode(param0: PorterDuffXfermode): PorterDuffXfermode;
8990
public getXfermode(): PorterDuffXfermode;
91+
public setColorFilter(param0: ColorFilter): void;
9092
}
9193

9294
// export class StaticLayout {
@@ -303,6 +305,7 @@ export class FillType extends android.graphics.Path.FillType {}
303305
export class Matrix extends android.graphics.Matrix {
304306
mapRect(rect: Rect);
305307
}
308+
export class ColorMatrixColorFilter extends android.graphics.ColorMatrixColorFilter {}
306309
export class PathEffect extends android.graphics.PathEffect {}
307310
export class DashPathEffect extends android.graphics.DashPathEffect {
308311
constructor(intervals: number[], phase: number);

Diff for: src/canvas.ios.ts

+52-2
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ export class Paint implements IPaint {
866866
color: Color;
867867
};
868868
shader;
869+
colorFilter;
869870
pathEffect: PathEffect;
870871
xfermode: IPorterDuffXfermode;
871872
mTextAttribs: NSMutableDictionary<any, any>;
@@ -967,6 +968,9 @@ export class Paint implements IPaint {
967968
public getShader() {
968969
return this.shader;
969970
}
971+
public getColorFilter() {
972+
return this.colorFilter;
973+
}
970974
public setStrokeWidth(value: number): void {
971975
this.strokeWidth = value;
972976
}
@@ -1003,6 +1007,12 @@ export class Paint implements IPaint {
10031007
}
10041008
this.shader = value;
10051009
}
1010+
public setColorFilter(value: any) {
1011+
if (this.colorFilter) {
1012+
this.colorFilter.release();
1013+
}
1014+
this.colorFilter = value;
1015+
}
10061016
setFont(font: Font) {
10071017
if (font === this.mFont) {
10081018
return;
@@ -1145,6 +1155,10 @@ export class Paint implements IPaint {
11451155
this.shader.clear();
11461156
this.shader = null;
11471157
}
1158+
if (this.colorFilter) {
1159+
this.colorFilter.clear();
1160+
this.colorFilter = null;
1161+
}
11481162
}
11491163
public setPathEffect(param0: PathEffect) {
11501164
this.pathEffect = param0;
@@ -1415,17 +1429,32 @@ export class Canvas implements ICanvas {
14151429
if (!image) {
14161430
return;
14171431
}
1432+
const length = args.length;
1433+
const paint = args[length - 1] as Paint;
1434+
let cgImage;
1435+
if (paint?.colorFilter) {
1436+
const ciFilter = paint.colorFilter.ciFilter;
1437+
const tmp = CIImage.alloc().initWithImage(image);
1438+
ciFilter.setValueForKey(tmp, 'inputImage');
1439+
1440+
const outputRect = tmp.extent;
1441+
const context = CIContext.contextWithOptions(null);
1442+
cgImage = context.createCGImageFromRect(ciFilter.outputImage, outputRect);
1443+
// image = UIImage.imageWithCGImageScaleOrientation(cgim, image.scale, image.imageOrientation);
1444+
} else {
1445+
cgImage = image.CGImage;
1446+
}
14181447

14191448
if (args[1] instanceof Matrix) {
14201449
CGContextConcatCTM(ctx, args[1]._transform);
14211450
CGContextTranslateCTM(ctx, 0, image.size.height);
14221451
CGContextScaleCTM(ctx, 1.0, -1.0);
1423-
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
1452+
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), cgImage);
14241453
} else {
14251454
const dst = args[2] instanceof Rect ? args[2].cgRect : CGRectMake(args[1], args[2], image.size.width, image.size.height);
14261455
CGContextTranslateCTM(ctx, 0, dst.origin.y + dst.size.height);
14271456
CGContextScaleCTM(ctx, 1.0, -1.0);
1428-
CGContextDrawImage(ctx, CGRectMake(dst.origin.x, 0, dst.size.width, dst.size.height), image.CGImage);
1457+
CGContextDrawImage(ctx, CGRectMake(dst.origin.x, 0, dst.size.width, dst.size.height), cgImage);
14291458
}
14301459
}
14311460

@@ -2156,6 +2185,27 @@ export class LinearGradient {
21562185
}
21572186
}
21582187
}
2188+
export class ColorMatrixColorFilter {
2189+
_ciFilter;
2190+
constructor(private values: number[]) {}
2191+
get ciFilter() {
2192+
if (!this._ciFilter) {
2193+
this._ciFilter = CIFilter.filterWithName('CIColorMatrix');
2194+
const value = this.values;
2195+
this._ciFilter.setValueForKey(CIVector.vectorWithValuesCount(new FloatConstructor(value.slice(0, 4)).buffer as any, 4), 'inputRVector');
2196+
this._ciFilter.setValueForKey(CIVector.vectorWithValuesCount(new FloatConstructor(value.slice(5, 9)).buffer as any, 4), 'inputGVector');
2197+
this._ciFilter.setValueForKey(CIVector.vectorWithValuesCount(new FloatConstructor(value.slice(10, 14)).buffer as any, 4), 'inputBVector');
2198+
this._ciFilter.setValueForKey(CIVector.vectorWithValuesCount(new FloatConstructor(value.slice(15, 19)).buffer as any, 4), 'inputAVector');
2199+
this._ciFilter.setValueForKey(CIVector.vectorWithValuesCount(new FloatConstructor([value[4], value[9], value[14], value[19]]).buffer as any, 4), 'inputBiasVector');
2200+
}
2201+
return this._ciFilter;
2202+
}
2203+
release() {
2204+
if (this._ciFilter) {
2205+
this._ciFilter = undefined;
2206+
}
2207+
}
2208+
}
21592209
export class RadialGradient {
21602210
_gradient;
21612211
constructor(public centerX: number, public centerY: number, public radius: number, public colors: any, public stops: any, public tileMode: TileMode) {}

0 commit comments

Comments
 (0)