LeadTools vector to grayscale conversion

I had the need to display a LeadTools VECTORHANDLE in grayscale (or even monochrome) over displaying everything in their natural colors. This is normally based on the need that some viewers have a black background and some white, and you can imagine that black on black or white on white lines are hard to see.

LeadTools doesn’t currently support drawing in monochrome or grayscale, so I had to write code to do it myself.

I do this by copying the VECTORHANDLE to another one (L_VecCopy), then converting that one to grayscale. I only have need for groups, polylines, polygons, lines, arcs, circles and text in my drawings, but the other supported types can be added easily enough.

Pass your VECTORHANDLE to the function GrayScaleVectorFile() and you are done. You will need to modify it if you intend to pass a custom pUserData.

function GrayScaleColor (InColor : Integer) : Integer;
// a color to greyscale algorithm I found online
var
GrayShade, Red, Green, Blue : Byte;
PixelColor : Integer;
begin
PixelColor := ColorToRGB (InColor);
Red := PixelColor;
Green := PixelColor shr 8;
Blue := PixelColor shr 16;
Grayshade := Round(0.3 * Red + 0.6 * Green + 0.1 * Blue);
Result := RGB(Grayshade, Grayshade, Grayshade);
end; { GrayScaleColor }
function ProcessVectorGrayScale (pVector : pVECTORHANDLE; pObject : pVECTOROBJECT; pUserData : Pointer) : Integer; far; stdcall;
var
vp : VECTORPOLYLINE;
Vo : VECTORPOLYGON;
Vl : VECTORLINE;
Va : VECTORARC;
Vc : VECTORCIRCLE;
Vt : VECTORTEXT;
Vclone : VECTORCLONE;
begin
with PAxSketchData(pUserData)^.CurFile, CurPage do begin
case pObject.nType of
VECTOR_CLONE : begin
L_VecGetObject (pVector, pObject, pObject.nType, @Vclone);
Vclone.Pen.Pen.elpColor := GrayScaleColor (Vclone.Pen.Pen.elpColor);
L_VecSetObject (pVector, pObject, pObject.nType, @Vclone);
end; { VECTOR_CLONE }
VECTOR_POLYLINE : begin
L_VecGetObject (pVector, pObject, pObject.nType, @Vp);
Vp.Pen.Pen.elpColor := GrayScaleColor (Vp.Pen.Pen.elpColor);
L_VecSetObject (pVector, pObject, pObject.nType, @Vp);
end; { VECTOR_POLYLINE }
VECTOR_POLYGON : begin
L_VecGetObject (pVector, pObject, pObject.nType, @Vo);
Vo.Pen.Pen.elpColor := GrayScaleColor (Vo.Pen.Pen.elpColor);
L_VecSetObject (pVector, pObject, pObject.nType, @Vo);
end; { VECTOR_POLYGON }
VECTOR_LINE : begin
L_VecGetObject (pVector, pObject, pObject.nType, @Vl);
Vl.Pen.Pen.elpColor := GrayScaleColor (Vl.Pen.Pen.elpColor);
L_VecSetObject (pVector, pObject, pObject.nType, @Vl);
end; { VECTOR_LINE }
VECTOR_ARC : begin
L_VecGetObject (pVector, pObject, pObject.nType, @Va);
Va.Pen.Pen.elpColor := GrayScaleColor (Va.Pen.Pen.elpColor);
L_VecSetObject (pVector, pObject, pObject.nType, @Va);
end; { VECTOR_ARC }
VECTOR_CIRCLE : begin
L_VecGetObject (pVector, pObject, pObject.nType, @Vc);
Vc.Pen.Pen.elpColor := GrayScaleColor (Vc.Pen.Pen.elpColor);
L_VecSetObject (pVector, pObject, pObject.nType, @Vc);
end; { VECTOR_CIRCLE }
VECTOR_TEXT : begin
L_VecGetObject (pVector, pObject, pObject.nType, @Vt);
Vt.Pen.Pen.elpColor := GrayScaleColor (Vt.Pen.Pen.elpColor);
L_VecSetObject (pVector, pObject, pObject.nType, @Vt);
end;{ VECTOR_TEXT }
end; { case }
end; { with }
Result := SUCCESS;
end; { ProcessVectorGrayScale }
function ProcessVectorObjectsInGroupGrayScale (pVector : pVECTORHANDLE; pObject : pVECTOROBJECT; pUserData : Pointer) : Integer; far; stdcall;
begin
Result := ProcessVectorGrayScale (pVector, pObject, pUserData);
end; { ProcessVectorObjectsInGroupGrayScale }
function ProcessVectorGroupsGrayScale (pVector : pVECTORHANDLE; pGroup : pVECTORGROUP; pUserData : Pointer) : Integer; far; stdcall;
var
Grp : VECTORGROUPDESC;
begin
FillChar (Grp, SizeOf(VECTORGROUPDESC), #0);
if L_VecGetGroup (pVector, pGroup, @Grp) = SUCCESS then
L_VecEnumObjectsInGroup (pVector, pGroup, @ProcessVectorObjectsInGroupGrayScale, nil, 0);
 Result := SUCCESS;
end; { ProcessVectorGroupsGrayScale }
procedure GrayScaleVectorFile (pVector : pVECTORHANDLE);
begin
L_VecEnumObjects (pVector, @ProcessVectorGrayScale, nil, 0);
L_VecEnumGroups (pVector, @ProcessVectorGroupsGrayScale, nil);
end; { GrayScaleVectorFile }