Skip to content

Commit

Permalink
Add support for parsing rgb color specifications.
Browse files Browse the repository at this point in the history
If rgba is provided, a warning message is produced since alpha values
must be specified speparately using an opacity attribute.

Issue #513
  • Loading branch information
dkoes committed May 11, 2021
1 parent 1101264 commit ee42326
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
18 changes: 16 additions & 2 deletions 3Dmol/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ var htmlColors = $3Dmol.htmlColors = {
// in an attempt to reduce memory overhead, cache all $3Dmol.Colors
// this makes things a little faster
$3Dmol.CC = {
rgbRegEx : /rgb(a?)\(\s*([^ ,\)\t]+)\s*,\s*([^ ,\)\t]+)\s*,\s*([^ ,\)\t]+)/i,
cache : {0:new $3Dmol.Color(0)},
color : function color_(hex) {
// Undefined values default to black
Expand Down Expand Up @@ -236,9 +237,22 @@ $3Dmol.CC = {
if(hex.length == 7 && hex[0] == '#') {
return parseInt(hex.substring(1),16);
}
else {
return htmlColors[hex.toLowerCase()] || 0x000000;

let m = this.rgbRegEx.exec(hex);
if(m) {
if(m[1] != "") {
console.log("WARNING: Opacity value in rgba ignored. Specify separately as opacity attribute.");
}
let ret = 0;
for(let i = 2; i < 5; i++) {
ret *= 256;
let val = m[i].endsWith("%") ? 255*parseFloat(m[i])/100 : parseFloat(m[i]);
ret += Math.round(val);
}
return ret;
}
return htmlColors[hex.toLowerCase()] || 0x000000;

}
return hex;
}
Expand Down
Binary file added tests/auto/imgs/testrgba.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/auto/tests/testrgba.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
viewer.addSphere({center:{x:0,y:0,z:0},radius:10.0,color:' rgb(100% ,0, 128)'});
viewer.addSphere({center:{x:10,y:0,z:0},radius:10.0,color:'rgba(1% , 128, 50%, .5) '});

0 comments on commit ee42326

Please sign in to comment.