Skip to content

Commit

Permalink
moved color logic to new js file
Browse files Browse the repository at this point in the history
  • Loading branch information
bhofmei committed Nov 11, 2016
1 parent 94ea7b5 commit 180b011
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 33 deletions.
72 changes: 72 additions & 0 deletions js/View/ColorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
define('NucleotideDensityPlugin/View/ColorHandler',[
'dojo/_base/declare',
'dojo/_base/array',
'dojo/_base/lang',
'dojox/color',
'dojo/_base/Color',
],
function(
declare,
array,
lang,
dojoxColor,
dojoColor

){
/*
Utility class dealing with degenerate nucleotide sequences
*/
var ColorHandler;

ColorHandler = {

generateRandomColors: function( labels ){
// take in list of labels and return object with equidistant colors
var s=100, l=65, sep=360/labels.length;
var hs=[];
var i;
for(i=0; i < labels.length; i++){
hs.push({name: labels[i], value: sep*i});
}
var colors ={};
array.forEach(hs, function(h){
var t = dojoxColor.fromHsl(h.value, s, l);
colors[h.name] =t.toHex();
});
return colors;
},

getFontColor: function(color){
// from http:https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color
var bg = new dojoColor(color);
var rgb = bg.toRgb();
var a = 1 - ( 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2])/255;
if(a >= 0.5)
return '#F0F0F0';
else
return '#010101';
},

getConfigColor: function( seqCtx, contextConfig, colorConfig, randomColors ){
// random
if(colorConfig === 'random')
return randomColors[seqCtx]
// other string
else if(typeof colorConfig === 'string')
return colorConfig
// array
else if(Array.isArray(colorConfig)){
var j = array.indexOf(seqCtx, contextConfig);
j %= colorConfig.length;
return colorConfig[j];
}
// object
else if(colorConfig.hasOwnProperty(seqCtx))
return colorConfig[seqCtx]
else
return randomColors[seqCtx]
}

}
return ColorHandler;
});
45 changes: 12 additions & 33 deletions js/View/Track/NucleotideDensity.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ define([
'dojo/_base/lang',
'dojo/_base/Color',
'dojox/color',
'NucleotideDensityPlugin/Store/SeqFeature/NucDensityMulti',
'JBrowse/View/Track/Wiggle/Density',
'JBrowse/Util',
'NucleotideDensityPlugin/Store/SeqFeature/NucDensityMulti',
'NucleotideDensityPlugin/View/ColorHandler',
'NucleotideDensityPlugin/View/Dialog/NucleotideDensityDialog'
],
function(
Expand All @@ -15,9 +16,10 @@ function(
lang,
Color,
dojoxColor,
NucContent,
WiggleDensity,
Util,
NucContent,
ColorHandler,
NucDensDialog
) {
return declare(WiggleDensity, {
Expand All @@ -30,11 +32,11 @@ function(
bothStrands: this.config.bothStrands,
contexts: this.config.context
});
var tmp = [''].concat(this.config.context);
//var tmp = [''].concat(this.config.context);
this.labels = array.map(this.config.context, function(ctx){
return {name: ctx};
});
this.randomColors = this._generateRandomColors(this.config.context);
this.randomColors = ColorHandler.generateRandomColors(this.config.context);
},

_defaultConfig: function() {
Expand All @@ -55,7 +57,7 @@ function(
});
},

getConfigColor: function( seqCtx ){
/*getConfigColor: function( seqCtx ){
var color = this.config.colors;
// random
if(color === 'random')
Expand All @@ -74,6 +76,10 @@ function(
return color[seqCtx]
else
return this.randomColors[seqCtx]
},*/

getConfigColor: function(seqCtx){
return ColorHandler.getConfigColor(seqCtx, this.config.contexts, this.config.colors, this.randomColors);
},

getConfForFeature: function(opt, feature){
Expand All @@ -83,33 +89,6 @@ function(
return this.inherited(arguments);
},

getFontColor: function(color){
// from http:https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color
var bg = new Color(color);
var rgb = bg.toRgb();
var a = 1 - ( 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2])/255;
if(a >= 0.5)
return '#F0F0F0';
else
return '#010101';
},

_generateRandomColors: function( labels ){
// take in list of labels and return object with equidistant colors
var s=100, l=65, sep=360/labels.length;
var hs=[];
var i;
for(i=0; i < labels.length; i++){
hs.push({name: labels[i], value: sep*i});
}
var colors ={};
array.forEach(hs, function(h){
var t = dojoxColor.fromHsl(h.value, s, l);
colors[h.name] =t.toHex();
});
return colors;
},

_calculatePixelScores: function(canvasWidth, features, featureRects) {
var pixelValues = new Array(canvasWidth);
array.forEach(features, function(f, i) {
Expand Down Expand Up @@ -203,7 +182,7 @@ function(
width: thisB.config.labelWidth ? thisB.config.labelWidth + 'px' : null,
font: thisB.config.labelFont,
backgroundColor: bg,
color: thisB.getFontColor(bg)
color: ColorHandler.getFontColor(bg)
},
innerHTML: elt.name,
title: elt.name
Expand Down

0 comments on commit 180b011

Please sign in to comment.