gpt4 book ai didi

计算两种颜色的 "average"

转载 作者:太空狗 更新时间:2023-10-29 15:27:42 27 4
gpt4 key购买 nike

这仅与编程相关 - 与颜色及其表示有更多关系。

我正在开发一个非常底层的应用程序。我在内存中有一个字节数组。那些是字符。它们是用抗锯齿渲染的:它们的值从 0 到 255,0 表示完全透明,255 完全不透明(alpha,如果您愿意)。

我无法构思用于呈现此字体的算法。我正在为每个像素执行以下操作:

            // intensity is the weight I talked about: 0 to 255
intensity = glyphs[text[i]][x + GLYPH_WIDTH*y];
if (intensity == 255)
continue; // Don't draw it, fully transparent
else if (intensity == 0)
setPixel(x + xi, y + yi, color, base); // Fully opaque, can draw original color
else { // Here's the tricky part
// Get the pixel in the destination for averaging purposes
pixel = getPixel(x + xi, y + yi, base);
// transfer is an int for calculations
transfer = (int) ((float)((float) (255.0 - (float) intensity/255.0) * (float) color.red + (float) pixel.red)/2); // This is my attempt at averaging
newPixel.red = (Byte) transfer;
transfer = (int) ((float)((float) (255.0 - (float) intensity/255.0) * (float) color.green + (float) pixel.green)/2);
newPixel.green = (Byte) transfer;
// transfer = (int) ((float) ((float) 255.0 - (float) intensity)/255.0 * (((float) color.blue) + (float) pixel.blue)/2);
transfer = (int) ((float)((float) (255.0 - (float) intensity/255.0) * (float) color.blue + (float) pixel.blue)/2);
newPixel.blue = (Byte) transfer;
// Set the newpixel in the desired mem. position
setPixel(x+xi, y+yi, newPixel, base);
}

如您所见,结果并不尽如人意。这是一个非常放大的图像,在 1:1 的比例下,文字看起来像是有绿色的“光环”。

The test, less than great

任何关于如何正确计算它的想法将不胜感激。

感谢您的宝贵时间!

最佳答案

您需要混合背景色和前景色。阿拉:

pixelColour = newColour * intensity + backgroundColour * (1 - intensity)

顺便说一句,这是一种非常缓慢的呈现和混合字体的方式。您应该改为将字体的所有字符渲染到具有您需要的所有属性的屏幕外表面,然后在需要文本时将其用作纹理以渲染到其他表面。

编辑:

这看起来不对:

(255.0 - (float) intensity/255.0)

它应该是:

(255.0 - (float) intensity)/255.0

关于计算两种颜色的 "average",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4566835/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com