-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blockly.FieldNumber does not support HEX #1306
Comments
oops ,I closed it .... what should I do now ? |
I reopened it for you, but your code doesn't do what you think it does. Please test any solutions you have with a variety of inputs to make sure they work. |
And just a note for anyone else looking at this, the feature request is to support 3-letter hex color codes. 6-letter hex color codes already work correctly. |
i think this is a number , not a color. |
Oh, you're right. This is the number field you were looking at. Sorry I misunderstood, I was looking at the code in develop instead of master. This was actually fixed by Neil's change to use Number everywhere instead of parseFloat. If you test on develop you'll see '0xff' gets parsed correctly and becomes 255 in FieldNumber. Closing as this issue is fixed in develop. =) |
I made a optimization to allow use input hex in FieldNumber
Blockly.FieldNumber.prototype.HEXExp = /^0xX+$/g;
/**
*/
Blockly.FieldNumber.prototype.classValidator = function(text) {
if (text === null) {
return null;
}
text = String(text);
// TODO: Handle cases like 'ten', '1.203,14', etc.
// 'O' is sometimes mistaken for '0' by inexperienced users.
text = text.replace(/O/ig, '0');
// Strip out thousands separators.
text = text.replace(/,/g, '');
var hex = text.match(Blockly.FieldNumber.prototype.HEXExp);
var n = hex ? parseInt(text, 16) : parseFloat(text || 0);
if (isNaN(n)) {
// Invalid number.
return null;
}
// Round to nearest multiple of precision.
if (this.precision_ && isFinite(n)) {
n = Math.round(n / this.precision_) * this.precision_;
}
// Get the value in range.
n = goog.math.clamp(n, this.min_, this.max_);
return hex ? text : String(n);
};
The text was updated successfully, but these errors were encountered: