平均颜色(X11 颜色)

发布于 2024-10-01 19:01:15 字数 75 浏览 4 评论 0原文

我想用平均颜色填充两个(或更多填充的)矩形的交集。我将每个矩形的颜色存储为无符号整数。如何获得平均颜色?

谢谢你的帮助!

I want to fill the intersection of two(or more filled) rectangles with the average color. I have the colors of each rectangle stored as unsigned ints. How can I get the average color?

Thank you for you help!

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

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

发布评论

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

评论(2

玩物 2024-10-08 19:01:15

从技术上讲,您可能在颜色映射设备上运行,这意味着您需要通过 X11 颜色管理来实现所有这些。您需要查询 XColor 以获得两种输入颜色,计算平均值,然后查找最接近的可表示颜色:

// Query XColor for both input colors
XColor xcol1, xcol2, outcol;
xcol1.pixel = color1;
xcol2.pixel = color2;
XQueryColor(display, colormap, &xcol1);
XQueryColor(display, colormap, &xcol2);

// Average red/green/blue and look up nearest representable color
outcol.red = (xcol1.red + xcol2.red) / 2;
outcol.green = (xcol1.green + xcol2.green) / 2;
outcol.blue = (xcol1.blue + xcol2.blue) / 2;
XAllocColor(display, colormap, &outcol);

// outcol.pixel is now the color to use

在调色板设备上,您还需要在之后释放颜色等 - 这是一团糟, 基本上。

但很可能您使用的是 32 位真彩色设备,这意味着整数只是 r、g、b 和 a 的位域(不一定按此顺序)。您可以这样计算它们的平均值:

UInt out_color = 0;
for (int i=0; i < 4; i++) {
  // Extract channel i from both input colors
  UInt in1 = (color1 >> (i*8)) & 0xff;
  UInt in2 = (color2 >> (i*8)) & 0xff;

  // Compute the average and or it into the output color
  out_color |= ((in1 + in2) / 2) << (i*8);
}

Technically, you might be running on a color-map device, which means you need to go through X11 color management for all of this. You need to query the XColor for your two input colors, compute the average, then look up the closest representable color:

// Query XColor for both input colors
XColor xcol1, xcol2, outcol;
xcol1.pixel = color1;
xcol2.pixel = color2;
XQueryColor(display, colormap, &xcol1);
XQueryColor(display, colormap, &xcol2);

// Average red/green/blue and look up nearest representable color
outcol.red = (xcol1.red + xcol2.red) / 2;
outcol.green = (xcol1.green + xcol2.green) / 2;
outcol.blue = (xcol1.blue + xcol2.blue) / 2;
XAllocColor(display, colormap, &outcol);

// outcol.pixel is now the color to use

On a paletted device, you also need to free the color afterwards etc. - it's a mess, basically.

But in all likelihood you're on a 32-bit truecolor device, which means the integer is just a bitfield of r, g, b and a (not necessarily in that order). You can compute their average like this:

UInt out_color = 0;
for (int i=0; i < 4; i++) {
  // Extract channel i from both input colors
  UInt in1 = (color1 >> (i*8)) & 0xff;
  UInt in2 = (color2 >> (i*8)) & 0xff;

  // Compute the average and or it into the output color
  out_color |= ((in1 + in2) / 2) << (i*8);
}
她比我温柔 2024-10-08 19:01:15
Color color1 = Color.FromArgb(UInt1);
Color color2 = Color.FromArgb(UInt2);
Color averageColor = Color.FromArgb(255,(color1.R + color2.R)/2,(color1.G + color2.G)/2,(color1.B + color2.B)/2);

这是假设您需要完全不透明的平均颜色。

Color color1 = Color.FromArgb(UInt1);
Color color2 = Color.FromArgb(UInt2);
Color averageColor = Color.FromArgb(255,(color1.R + color2.R)/2,(color1.G + color2.G)/2,(color1.B + color2.B)/2);

This is assuming that you need a fully opaque average color.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文