Skip to content

Commit 0066af6

Browse files
committed
add helper to find link color and update setColorOptions
1 parent 707bfc5 commit 0066af6

File tree

1 file changed

+61
-10
lines changed

1 file changed

+61
-10
lines changed

src/main.js

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function TidyTree(data, options, events) {
1111
layout: "vertical",
1212
type: "tree",
1313
mode: "smooth",
14-
colorOptions: { colorMode: "none" },
14+
colorOptions: { nodeColorMode: "none" },
1515
leafNodes: true,
1616
leafLabels: false,
1717
equidistantLeaves: false,
@@ -110,7 +110,13 @@ TidyTree.validModes = ["smooth", "square", "straight"];
110110
* The available color modes for rendering nodes.
111111
* @type {Array}
112112
*/
113-
TidyTree.validColorModes = ["none", "list"]; // later, highlight on hover, or maybe color by annotation on a node/ search
113+
TidyTree.validNodeColorModes = ["none", "list"]; // later, highlight on hover, or maybe color by annotation on a node/ search
114+
115+
/**
116+
* The available color modes for rendering branches.
117+
* @type {Array}
118+
*/
119+
TidyTree.validLinkColorModes = ["none", "monophyletic"]; // later, toRoot?
114120

115121
/**
116122
* Draws a Phylogenetic on the element referred to by selector
@@ -374,22 +380,54 @@ nodeTransformers.dendrogram = nodeTransformers.tree;
374380
* @return {string} The color of the node.
375381
*/
376382
function findNodeColor(node, colorOptions) {
377-
if (colorOptions.colorMode === "none") {
383+
if (colorOptions.nodeColorMode === "none") {
378384
// steelblue
379385
return colorOptions.defaultColor ?? "#4682B4";
380386
}
381387

382388
let nodeList = colorOptions.nodeList;
383389

384390
if (nodeList.includes(node.data._guid)) {
385-
// charcoal
391+
// yellowish
386392
return colorOptions.highlightColor ?? "#feb640";
387393
} else {
388-
// yellowish
394+
// charcoal
389395
return colorOptions.defaultColor ?? "#243127";
390396
}
391397
}
392398

399+
/**
400+
* Find the color of a given link based on the provided color options.
401+
*
402+
* @param {string} link - The link for which to find the color.
403+
* @param {object} colorOptions - The options for different link colors.
404+
* @return {string} The color of the link.
405+
*/
406+
function findLinkColor(link, colorOptions) {
407+
if (colorOptions.linkColorMode === "none") {
408+
// charcoal
409+
return colorOptions.defaultColor ?? "#243127";
410+
}
411+
let source = link.source;
412+
413+
// todo modify this to find all leaves rather than all children
414+
// find all children of the source
415+
let children = source.children;
416+
417+
// check if all children are in the node list
418+
let allChildrenInNodeList = children.every(child => colorOptions.nodeList.includes(child.data._guid));
419+
420+
// if all of the children are in the node list, return the highlight color
421+
if (allChildrenInNodeList) {
422+
// yellowish
423+
return colorOptions.highlightColor ?? "#feb640";
424+
}
425+
426+
// otherwise, return the default color
427+
return colorOptions.defaultColor ?? "#243127";
428+
}
429+
}
430+
393431
const radToDeg = 180 / Math.PI;
394432

395433
let labelTransformers = {
@@ -807,17 +845,30 @@ TidyTree.prototype.setLayout = function (newLayout) {
807845
* @return {TidyTree} The TidyTree Object
808846
*/
809847
TidyTree.prototype.setColorOptions = function (newColorOptions) {
810-
if (!TidyTree.validColorModes.includes(newColorOptions.colorMode)) {
848+
if (!TidyTree.validNodeColorModes.includes(newColorOptions.nodeColorMode)) {
811849
throw Error(`
812-
Cannot set TidyTree to colorOptions: ${newColorOptions.colorMode}\n
813-
Valid colorModes are: ${TidyTree.validColorModes.join(', ')}
850+
Cannot set TidyTree colorOptions: ${newColorOptions.nodeColorMode}\n
851+
Valid nodeColorModes are: ${TidyTree.validnodeColorModes.join(', ')}
814852
`);
815853
}
816-
if (newColorOptions.colorMode === 'list') {
854+
if (!TidyTree.validLinkColorModes.includes(newColorOptions.linkColorMode)) {
855+
throw Error(`
856+
Cannot set TidyTree colorOptions: ${newColorOptions.linkColorMode}\n
857+
Valid linkColorModes are: ${TidyTree.validLinkColorModes.join(', ')}
858+
`);
859+
}
860+
861+
if (newColorOptions.nodeColorMode === 'list') {
817862
if (!Array.isArray(newColorOptions.nodeList)) {
818-
throw Error('nodeList must be an array for colorMode "list"');
863+
throw Error('nodeList must be an array for nodeColorMode "list"');
864+
}
865+
} else {
866+
// nodeColorMode === 'none'
867+
if (newColorOptions.linkColorMode !== 'none') {
868+
throw Error('linkColorMode must be "none" for nodeColorMode "none"');
819869
}
820870
}
871+
821872
this.colorOptions = newColorOptions;
822873
if (this.parent) return this.redraw();
823874
return this;

0 commit comments

Comments
 (0)