我正在编写一个与dxf文件交互的程序。因此,我需要一种例程,它可以接收RGB颜色值,并返回最接近AutoCAD颜色索引(ACI)的颜色。
有没有人有相关代码或示例呢?最好是用C#编写,但这并不是必须的。
提前感谢。
dist = (r-R)*(r-R) + (g-G)*(g-G) + (b-B)*(b-B);
以下是将RGB转换为ACI的方法
var color = Autodesk.AutoCAD.Colors.Color(r, g, b);
我不会像安德斯建议的那样麻烦地写一个硬编码的ACI颜色数组。你可以从每个合法索引获取一个AutoCAD Color
对象,并从中提取RGB值作为System.Drawing.Color
,使用ColorValue
属性。
这是基于安德斯响应的其余部分的完整解决方案,将(r - R)*(r - R)
替换为Math.Pow(r - R, 2)
,因为它似乎更清楚地表达了“距离”计算的勾股意图。
byte r = 1, g = 203, b = 103; // input color
double minDist = double.MaxValue;
short match = 0; // this will end up with our answer
for (short i = 1; i <= 255; ++i)
{
var color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, i);
System.Drawing.Color rgb = color.ColorValue;
double dist =
Math.Pow(r - rgb.R, 2) +
Math.Pow(g - rgb.G, 2) +
Math.Pow(b - rgb.B, 2);
if (dist < minDist)
{
minDist = dist;
match = i;
}
}
我不确定这个帖子/问题是否仍然有效,但我一直在网上寻找将颜色转换为ACI的方法,但没有成功。在我的情况下,我需要一种最好避免使用外部库和CAD函数的方法。
我无法提供C#的帮助。我通常使用Lazarus/Free Pascal,并经过大量尝试,我得出了一个似乎对我而言工作得非常好的函数。因此,我在此发布我的代码,以防它们对您或其他人有所帮助。
我的代码如下:
Function RGB2ACIDXFColor(MyColor : TColor) : Integer ;
Var
OldCol, LowR, MidR, HiR : String ;
RCol, GCol, BCol, LowCol, MidCol, HiCol : Integer ;
StPt, HRatio, VRatio, Hemis : Integer ;
Begin
Result := 10 ;
{Break Color Component (BGR Color)}
{IntToHex & Hex2Dec are functions from Lazarus Libraries}
OldCol := IntToHex(MyColor,6) ;
BCol := Hex2Dec(Copy(OldCol,1,2)) ;
GCol := Hex2Dec(Copy(OldCol,3,2)) ;
RCol := Hex2Dec(Copy(OldCol,5,2)) ;
{Find Color Component Priorities}
LowCol := RCol ;
LowR := 'R' ;
If (GCol < LowCol) Then
Begin
LowCol := GCol ;
LowR := 'G' ;
End; //If
If (BCol < LowCol) Then
Begin
LowCol := BCol ;
LowR := 'B' ;
End; //If
HiCol := RCol ;
HiR := 'R' ;
If (GCol > HiCol) Then
Begin
HiCol := GCol ;
HiR := 'G' ;
End; //If
If (BCol > HiCol) Then
Begin
HiCol := BCol ;
HiR := 'B' ;
End; //If
MidCol := GCol ;
MidR := 'G' ;
If ((HiR = 'G') AND (LowR = 'R')) OR
((HiR = 'R') AND (LowR = 'G')) Then
Begin
MidCol := BCol ;
MidR := 'B' ;
End; //If
If ((HiR = 'G') AND (LowR = 'B')) OR
((HiR = 'B') AND (LowR = 'G')) Then
Begin
MidCol := RCol ;
MidR := 'R' ;
End; //If
{Refer to CAD color table}
{Find Color Row}
VRatio := Round((5 * (255 - HiCol)) / 255) ;
VRatio *= 2 ;
{Find Color Hemisphere}
If (LowCol = 0) Then Hemis := 0 Else Hemis := 1 ;
{Find Color Start Column And Incrementation}
If (LowR = 'B') Then
Begin
HRatio := Round((8 * GCol) / (GCol + RCol)) ;
Result := 10 ;
End; //If
If (LowR = 'G') Then
Begin
HRatio := Round((8 * RCol) / (RCol + BCol)) ;
Result := 170 ;
End; //If
If (LowR = 'R') Then
Begin
HRatio := Round((8 * BCol) / (BCol + GCol)) ;
Result := 90 ;
End; //If
HRatio *= 10 ;
Result += HRatio + VRatio + Hemis ;
If (Result > 249) Then Result -= 240 ;
End; //Sub
我相信你能将其翻译成C#,希望这对某人有用。
干杯,
J-Eric J.