Comments (6)
Thanks @brettfo for the detailed explanation. For anyone else converting custom RGB values to a best-guess color index, the following class might be useful.
public static class DxfColorHelpers
{
public static DxfColor GetClosestDefaultIndexColor(byte r, byte g, byte b)
{
int minDist = int.MaxValue;
int minIndex = -1;
ReadOnlyCollection<uint> colors = DxfColor.DefaultColors;
// index 0 is left out intentionally!
for (int i = 1; i < colors.Count; i++)
{
int sqd = SquaredColorDistance(r, g, b, colors[i]);
if (sqd == 0) // exact match
{
return DxfColor.FromIndex((byte)i);
}
if (sqd < minDist)
{
minDist = sqd;
minIndex = i;
}
}
return DxfColor.FromIndex((byte)minIndex);
}
private static int SquaredColorDistance(byte r, byte g, byte b, uint otherColor)
{
(byte r2, byte g2, byte b2) = ToRgb(otherColor);
return (r - r2) * (r - r2)
+ (g - g2) * (g - g2)
+ (b - b2) * (b - b2);
}
public static (byte, byte, byte) ToRgb(uint color)
{
//byte a = (byte)(color >> 24);
byte r = (byte)(color >> 16);
byte g = (byte)(color >> 8);
byte b = (byte)(color >> 0);
return (r, g, b);
}
public static uint FromRgb(byte r, byte g, byte b)
{
const byte a = 0xFF; // alpha not used
return (uint)(a << 24 | r << 16 | g << 8 | b << 0);
}
public static string ToHexString(this DxfColor color)
{
if (color.IsIndex)
{
uint argb = DxfColor.DefaultColors[color.Index];
return ToHexString(argb);
}
return color.ToString();
}
public static string ToHexString(uint color)
{
return "0x" + color.ToString("X8");
}
}
from dxf.
Yes, this is intentional; there is no concept of color 0
in DXF/DWG. As for finding the closest one, I can't think of anything better than the smallest squared distance using for (int i = 1; i < ...)
. That being said, the color numbers in AutoCAD can be overridden, so while color 1
is traditionally red, that's not guaranteed to be the case; the values specified in DxfColor.DefaultColors
are simply what AutoCAD defaults to on first install.
Starting with AutoCAD 2004, entities can separately specify a 24-bit color value which I expose as the property Color24Bit
, however I don't know what happens if the 24-bit color value and the index values differ.
from dxf.
A 24 bit color would perfectly match my scenario (and I would not set the index at all?). Unfortunately, the DxfLayer
doesn't have the 24 bit color property.
from dxf.
The spec only allows a color index for LAYER
, so you'd have to set a 24-bit value on each individual entity. As for specifying the 24-bit value vs. the index, the spec doesn't say if/when the 24-bit value is used. The only comments are:
Regarding the color index:
Color number (present if not BYLAYER); zero indicates the BYBLOCK (floating) color; 256 indicates BYLAYER; a negative value indicates that the layer is turned off (optional)
Regarding the 24-bit color:
A 24-bit color value that should be dealt with in terms of bytes with values of 0 to 255. The lowest byte is the blue value, the middle byte is the green value, and the third byte is the red value. The top byte is always 0. The group code cannot be used by custom entities for their own data because the group code is reserved for AcDbEntity, class-level color data and AcDbEntity, class-level transparency data
from dxf.
I'd take your helper functions as a PR if you think they'll be useful to you.
from dxf.
Not worth the effort for now - I'm experimenting with an export into a 3D dxf file for our customers... but this is really troublesome, especially because not all cad apps use / understand the same properties. If only a few more problems come up, this project is dead.
from dxf.
Related Issues (20)
- Add Aligned Dimension with text above the dimension line and how to increase text size? HOT 1
- spline closed attribute error HOT 3
- How to specify a font for Text? HOT 2
- SPATIAL_FILTER code 40 issue HOT 1
- Ellipse uses wrong extents calculation HOT 1
- From Spline to Dxf HOT 2
- MTEXT AutoCAD 2018 Embedded Object (code 101) issue
- Opening file error "Not a valid DXF file header: `?0`" HOT 9
- How to calculate spline length? HOT 2
- How to get an edge/block's connected units (up & downstream) HOT 1
- How to explicitly set DXF color to black? HOT 1
- Unsupported code pair of dxf is swallowed HOT 2
- load the library from IronPython HOT 2
- DxfEllipse - thickness missing HOT 6
- Mirrored coordinates of some entries. HOT 3
- Support for MLeader HOT 2
- how to read dxf file content text? HOT 1
- DxfHatch exception HOT 2
- polyline convert to line and arc with AsSimpleEntities method get wrong start/end angle HOT 3
- Approximate SPLINE entity as simple entities
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dxf.