xibyte / jsketcher

Parametric 2D and 3D modeler written in pure javascript
http://web-cad.org
Other
1.45k stars 275 forks source link

When export to dxf format, add support to colors base on the AutoCad … #190

Closed NetzerRosenfeld closed 1 year ago

NetzerRosenfeld commented 1 year ago

…colors (which using by the library of dxf)

Related issue: no support in colors in dxf

Description:

When exporting to dxf format, I added support to colors based on the AutoCAD table,

Contributor name: Netser Rosenfeld

Confirm compliance with JSketcher project license.

tarikjabiri commented 1 year ago

Hi,

I think this is not the better way to handle colors, because the appraoch used only working if the color is indexed. For example take the red color: index is 1 and the hex value is #ff0000, what if the hex value given is #ff0001, your algorithm won't work assuming the #ff0001 is not indexed but its also red.

So as a solution to support all colors not only the indexed ones we can use a property called the trueColor in layer, or directly on the entity, assuming if jsketcher is supporting colors for the shapes.

this is the better solution for now:

let currentLayer = this.writer.layer(layer.name);
if (!currentLayer) {
  currentLayer = this.writer.addLayer(layer.name, Colors.Black);
  const hexColor = layer.style.strokeStyle;
  currentLayer.trueColor = TrueColor.fromHex(hexColor); // Import TrueColor from @tarikjabiri/dxf lib.
}

And if we need to use only the indexed colors we need a mapping to the closest color in the index table. And this will be supported directly in dxf lib soon.

Regards

NetzerRosenfeld commented 1 year ago

Hi @tarikjabiri , I used the color index of the AutoCad only because it was required for your (awesome) dxf library, If I understood right, you said that by using the trueColor I can use the hex number directly? The Jsketcher already contains property of color here - layer.style.strokeStyle

tarikjabiri commented 1 year ago

Yeah the index colors are the main colors for AutoCAD but its not the only way. If you have the hex or the rgb values you can use them directly for true color property using the TrueColor helper class.

This from the dxf spec: using code 420

"32-bit integer value. When used with True Color; a 32-bit integer representing a 24-bit color value. The high-order byte (8 bits) is 0, the low-order byte an unsigned char holding the Blue value (0-255), then the Green value, and the next-to-high order byte is the Red Value. Converting this integer value to hexadecimal yields the following bit mask: 0x00RRGGBB. For example, a true color with Red==200, Green==100 and Blue==50 is 0x00C86432, and in DXF, in decimal, 13132850"

NetzerRosenfeld commented 1 year ago

Ok, so I will close the pull request.