[SOLVED] Valid colors, how to test?

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

[SOLVED] Valid colors, how to test?

Post by CVH » Mon Apr 26, 2021 7:14 am

This already bothers me a while:

Code: Select all

var colorString = "not-a-color";
var newColor = new RColor(colorString);
var msg = "\'%1\' is a valid color"
if (newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));
var msg = "\'%1\' is NOT a valid color"
if (!newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));

var colorString = "byLayer";
var newColor = new RColor(colorString);
var msg = "\'%1\' is a valid color"
if (newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));
var msg ="\'%1\' is NOT a valid color"
if (!newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));

var colorString = "#008000";    // Dark Green
var newColor = new RColor(colorString);
var msg = "\'%1\' is a valid color"
if (newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));
var msg = "\'%1\' is NOT a valid color"
if (!newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));

var colorString = "maroon";    // Dark Red
var newColor = new RColor(colorString);
var msg = "\'%1\' is a valid color"
if (newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));
var msg = "\'%1\' is NOT a valid color"
if (!newColor.isValid) EAction.handleUserMessage(msg.arg(colorString));

Reported is:
  • 'not-a-color' is a valid color ... (it isn't)
    'byLayer' is a valid color ... (it isn't)
    '#008000' is a valid color
    'maroon' is a valid color
How to test for valid colors as string?

Regards,
CVH
Last edited by CVH on Fri Apr 30, 2021 8:47 am, edited 1 time in total.

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: Valid colors, how to test?

Post by andrew » Mon Apr 26, 2021 8:34 am

isValid is a function:

Code: Select all

if (newColor.isValid()) ...
A function object evaluates to true if the function is defined. if (newColor.isValid) is always true.

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: Valid colors, how to test?

Post by CVH » Mon Apr 26, 2021 5:07 pm

Then I have to repeat the question:
How to test for valid colors as string?

Regards,
CVH

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: Valid colors, how to test?

Post by andrew » Mon Apr 26, 2021 8:08 pm

CVH wrote:
Mon Apr 26, 2021 5:07 pm
How to test for valid colors as string?

Code: Select all

valid = new RColor("something").isValid();
// valid is false

valid = new RColor("red").isValid();
// valid is true
Please note the brackets after isValid.

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: Valid colors, how to test?

Post by CVH » Mon Apr 26, 2021 8:49 pm

Noted, thanks.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”