Skip to content

Commit 547ba93

Browse files
committed
Create and use padHex function.
1 parent c27054e commit 547ba93

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/robotjs.cc

+19-9
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,18 @@ NAN_METHOD(setKeyboardDelay)
556556
557557
*/
558558

559+
/**
560+
* Pad hex color code with leading zeros.
561+
* @param color Hex value to pad.
562+
* @param hex Hex value to output.
563+
*/
564+
void padHex(MMRGBHex color, char* hex)
565+
{
566+
//Length needs to be 7 because snprintf includes a terminating null.
567+
//Use %06x to pad hex value with leading 0s.
568+
snprintf(hex, 7, "%06x", color);
569+
}
570+
559571
NAN_METHOD(getPixelColor)
560572
{
561573
MMBitmapRef bitmap;
@@ -567,12 +579,10 @@ NAN_METHOD(getPixelColor)
567579
bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, 1, 1));
568580

569581
color = MMRGBHexAtPoint(bitmap, 0, 0);
570-
571-
char hex [7];
572-
573-
//Length needs to be 7 because snprintf includes a terminating null.
574-
//Use %06x to pad hex value with leading 0s.
575-
snprintf(hex, 7, "%06x", color);
582+
583+
char hex[7];
584+
585+
padHex(color, hex);
576586

577587
destroyMMBitmap(bitmap);
578588

@@ -638,14 +648,14 @@ NAN_METHOD(getColor)
638648

639649
color = MMRGBHexAtPoint(bitmap, 300, 300);
640650

641-
char hex [7];
642651

643-
snprintf(hex, 7, "%06x", color);
644652

645653
printf("2: %s\n", hex);
646654

647-
//FIXME: Currently causes an error.
648655
//destroyMMBitmap(bitmap);
656+
char hex[7];
657+
658+
padHex(color, hex);
649659

650660
info.GetReturnValue().Set(Nan::New(hex).ToLocalChecked());
651661

0 commit comments

Comments
 (0)