- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个整数列表,通过 C++ 代码获得并代表不同的 D3DCOLORVALUE 环境等...
在 Delphi 中从这个数字获得相同颜色的正确方法是什么?
例如,使用 C++ (Visual Studio 2012) 中的程序,通过调用 C++ 中的库例程(单独的 dll 文件)我可以获得以下值:
-1.44852785e-016 = 2770796799 (represent the color)
1.57844226e+11 = 1376977151 (represent the color)
-1.98938735e+034 = 4168431103 (represent the color)
3.39617733e+038 = 4294967295 (represent the color)
@Rudy Velthuis,原代码是:(数组中包含的一些值就是上面显示的)
int32_t index = ifcObject->indicesForFaces[0];
uint32_t ambient = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 6],
diffuse = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 7],
emissive = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 8],
specular = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 9];
然后在Delphi XE6中使用
PInteger (@ColorsArray [index * sizeOfVertices + 6])^
我得到相同的值,但在 Delphi 中,颜色为黑色或空值。有时会得到负数,据了解,在 C++ 中可以用负值表示颜色,但在 Delphi 中不起作用。
如何将以 D3DCOLORVALUE 格式获得的颜色值转换为 Delphi 的 TColor?
这些值用于获取对象的颜色D3DMATERIAL9、D3DCOLORVALUE Ambient(环境色RGB)
FLOAT r;
FLOAT g;
FLOAT b;
FLOAT a;
这个函数使颜色:
void SetColor(
D3DCOLORVALUE * iColor,
uint32_t color
)
{
iColor->r = (float) (color & ((unsigned int) 255 * 256 * 256 * 256)) / (256 * 256 * 256);
iColor->r /= 255.f;
iColor->g = (float) (color & (255 * 256 * 256)) / (256 * 256);
iColor->g /= 255.f;
iColor->b = (float) (color & (255 * 256)) / 256;
iColor->b /= 255.f;
iColor->a = (float) (color & (255));
iColor->a /= 255.f;
}
但是在Delphi中不行,得到的颜色是错误的。
颜色与@Dsm 解决方案的比较:
左:获得的颜色 右:想要的颜色
我举了一个简单的例子来说明区别:
第一种颜色对应柱脚,第二种颜色对应柱子。带平台的图是带库查看器的示例,具有该版本的正确颜色,右图是 Visual Studio 2012 中 watch 的图像,显示数组的内容相同且值通过类型转换获得的结果也是相同的,但是,当将这些数字转换为颜色时,它们不会给出相同的结果。
最佳答案
这看起来像原始的 C++ 提取颜色,非常了解记录数组的内部结构。在你的代码中你不应该使用 (int32_t*) 因为它创建了一个指向常量的指针,而你只想将 float 转换为 uint_32_t,所以我认为你想要满足你的测试代码的只是
ambient := uint32_t(-1.44852785e-016);
diffuse := uint32_t(1.57844226e+11);
etc;
这不会在 Delphi 中编译(无效的类型转换),所以您需要做类似的事情
var
p : pointer;
iFloat : single;
iColour : TColor;
begin
iFloat := 1.57844226e+11;
p := @(iFloat);
iColour := TColor(p^);
diffuse := iColour;
end;
呈现红色。
请注意,您所有的颜色看起来都很相似,因为您使用的是几个相似地址的开头。
表示为一个函数,这可能会变成
function FloatToColor( const pVal : single ) : TColor;
var
p : pointer;
iFloat : single;
begin
iFloat := 1.57844226e+11;
p := @(iFloat);
Result := TColor(p^);
end;
根据 $FF 出现在错误位置的事实,我想到这些值可能是 CMYK 格式。在测试中,我发现它是一种经过修改的 CMYK 格式 - 如果您愿意的话,更像是一种 YMCK 格式,因此为了获得正确的颜色,我必须调换红色和蓝色的角色。
没有从 CMYK 到 RGB 的真正转换,但以下是近似值。
function CMYKtoRGB( const pValue : TColor) : TColor;
var
c,m,y,k,r,g,b : byte;
begin
c := GetCValue( pValue );
m := GetMValue( pValue );
y := GetYValue( pValue );
k := GetKValue( pValue );
//r := $FF xor c;
r := $FF - c;
b := $FF - y;
g := $FF - m;
if k <> $FF then
begin
k := $FF - k;
r := r * k;
g := g * k;
b := b * k;
end;
Result := RGB( b, g, r );
end;
function FloatToColor( const pVal : single ) : TColor;
var
p : pointer;
iFloat : single;
begin
iFloat := pVal;
p := @(iFloat);
Result := CMYKtoRGB(TColor(p^));
end;
请注意,我使用 RGB( b, g, r) 而不是预期的 RGB( r, g, b) 来解释颠倒的颜色顺序。
关于c++ - 如何将 D3DCOLORVALUE 转换为 delphi tcolor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788960/
我是一名优秀的程序员,十分优秀!