创建 ColorFilter 的自定义子类?

发布于 2024-10-06 08:33:07 字数 587 浏览 7 评论 0原文

好的,这与我之前关于 ColorMatrixColorFilter 的问题有些相关,但我觉得这是一个明显不同的问题。我想知道是否有办法 - 或者更确切地说,如何扩展 ColorFilter 类来创建我自己的自定义滤色器。对于我需要完成的任务,我需要编写一个自定义过滤器来查询每个像素,将其 RGB 值转换为 HSL 或 LAB,修改色调,将其转换回 RGB,然后将该像素设置为新值。

我想我可以简单地编写一个类来执行此操作,接受一个 Drawable 和一定量的色调偏移来执行,但必须为每个 Drawable 以及每个 Drawable 的每个状态手动调用它,而ColorFilter 似乎可以很好地处理这个问题。鉴于 LightingColorFilter 和 ColorMatrixColorFilter 的存在,似乎可以对它进行子类化,但到目前为止,我寻找任何类型文档的努力都是徒劳的。我似乎找不到这三个(Lighting、ColorMatrix、ColorFilter)中任何一个的源代码;我想它们可能是用本机代码完成的?

我的问题是:如何正确子类化 ColorFilter?如果我找不到一个好的答案,如果有人能够找到源代码(我搜索了 Android 的 git)并发布一个链接,那也会很有帮助。

谢谢!

Okay, so this is somewhat related to my previous question on the ColorMatrixColorFilter, but I feel it's a significantly different question. I'm wondering if there's a way - or rather, how to extend the ColorFilter class to create my own custom color filter. For what I'm needing to accomplish, I need to write a custom filter that will query each pixel, convert its RGB value to HSL or LAB, modify the hue, convert it back to RGB, and set that pixel to the new value.

I'm thinking I could simply write a class that does this, taking in a Drawable and an amount of hue shift to perform, but it would have to be called manually for each and every Drawable, and every state of every Drawable, whereas the ColorFilter seems to handle this nicely. Given the existence of the LightingColorFilter and ColorMatrixColorFilter, it seems like it can be subclassed, but so far my efforts to find any sort of documentation have been futile. I can't seem to find the source code for any of the three (Lighting, ColorMatrix, ColorFilter); I'm thinking they're probably done in native code?

My question is this: How can I properly subclass ColorFilter? If I can't find a good answer for that, if anyone is able to find the source (I've searched Android's git) and post a link to that, that would be helpful as well.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2024-10-13 08:33:07

正如您所说, 源代码显示 ColorFilter 使用本机代码,因此您无法真正对其进行子类化。

除了为您想做的事情创建自己的类之外,可能没有其他方法了。

As you said, the source code shows ColorFilter use native code so you cannot really subclass it.

There's probably no other way than creating your own class for what you want to do.

心意如水 2024-10-13 08:33:07

您可以使用它来应用您自己的滤色器技术,不幸的是,它适用于 RGB:

// The matrix is stored in a single array, and its treated as follows: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
// When applied to a color [r, g, b, a], the resulting color is computed as (after clamping) ;
//   R' = a*R + b*G + c*B + d*A + e;
//   G' = f*R + g*G + h*B + i*A + j;
//   B' = k*R + l*G + m*B + n*A + o;
//   A' = p*R + q*G + r*B + s*A + t;

Paint paint = new Paint();
float[] matrix = {
        1, 1, 1, 1, 1, //red
        0, 0, 0, 0, 0, //green
        0, 0, 0, 0, 0, //blue
        1, 1, 1, 1, 1 //alpha
};

paint.setColorFilter(new ColorMatrixColorFilter(matrix));

在我的情况下,我需要应用 HSL 效果,如 PhotoShop 中的 colorize,它不是 100% 正确,但这给出了很好的结果:

 float[] HSL = imageLayer.getColorize();
 PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(ColorUtils.HSLToColor(HSL),PorterDuff.Mode.MULTIPLY);
 paint.setColorFilter(colorFilter);

You may use this to apply your own color filter technique unfortunately works on RGB:

// The matrix is stored in a single array, and its treated as follows: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
// When applied to a color [r, g, b, a], the resulting color is computed as (after clamping) ;
//   R' = a*R + b*G + c*B + d*A + e;
//   G' = f*R + g*G + h*B + i*A + j;
//   B' = k*R + l*G + m*B + n*A + o;
//   A' = p*R + q*G + r*B + s*A + t;

Paint paint = new Paint();
float[] matrix = {
        1, 1, 1, 1, 1, //red
        0, 0, 0, 0, 0, //green
        0, 0, 0, 0, 0, //blue
        1, 1, 1, 1, 1 //alpha
};

paint.setColorFilter(new ColorMatrixColorFilter(matrix));

Any way in my case I was needed to apply HSL effect like colorize in PhotoShop it is not 100% correct but this gives nice result :

 float[] HSL = imageLayer.getColorize();
 PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(ColorUtils.HSLToColor(HSL),PorterDuff.Mode.MULTIPLY);
 paint.setColorFilter(colorFilter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文