Flutter用户使用矩阵对暗图像进行颜色过滤



我想在扑动用户ColorFiltered,所以我可以使jpg图像黑暗这里是我的代码,但我没有得到正确的结果,我不理解矩阵和如何改变颜色这个代码是从互联网,请任何想法将是受欢迎的。

ColorFiltered(
child: ColorFiltered(
child: Image.asset(
'myimage.jpg',
fit: BoxFit.fill,
),
colorFilter: ColorFilter.matrix(
[
//R  G   B    A  Const
-1, 0, 0, 0, 190, //
0, -1, 0, 0, 255, //
0, 0, -1, 0, 255, //
0, 0, 0, 1, 0, //
],
),
),
colorFilter: ColorFilter.mode(
Colors.white,
BlendMode.darken,
),
)

如果你只是想让你的图像更暗,更好的解决方案可能只是在你的图像上放一个透明的黑色Container和一个Stack小部件:

Stack(children: <Widget>[
Image.network('https://picsum.photos/250?image=9'),
Positioned.fill(
child: Opacity(
opacity: 0.5,
child: Container(
color: const Color(0xFF000000),
),
),
),
]),

如果你真的想使用ColorFiltered让我知道,但你可以这样做:

ColorFiltered(
child: Image.network('https://picsum.photos/250?image=9'),
colorFilter: const ColorFilter.mode(
Color(0xFF3D3D3D),
BlendMode.darken,
),
),

请记住,ColorFiltered将不只是在图像上添加一个新的灰色层。它分别变换图像的每个像素。根据文档:

这个小部件根据指定的ColorFilter,对子内容的每个像素独立应用一个函数。

这是基于我的搜索和一点重构的结果:

static ColorFilter brightnessAdjust(double value) {
if (value >= 0.0) {
value = value * 255;
} else {
value = value * 100;
}
List<double> matrix;
if (value == 0) {
matrix = identityMatrix;
} else {
matrix = [
1,
0,
0,
0,
value,
0,
1,
0,
0,
value,
0,
0,
1,
0,
value,
0,
0,
0,
1,
0
];
}
return ColorFilter.matrix(matrix);
}

正值变亮,负值变暗(-1到1),零不改变任何东西

最新更新