001/*
002 *                  BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Aug 3, 2007
021 */
022package org.biojava.nbio.structure.gui.util.color;
023
024/**
025 * Performs a log10 transform on input before passing the values off to another
026 * colorMapper.
027 *
028 * For instance, to map [10^0, 10^10] to a rainbow gradient, use
029 *
030 * new LogColorMapper(GradientMapper.getGradientMapper(GradientMapper.RAINBOW_GRADIENT, 0, 10))
031 *
032 * @author Spencer Bliven
033 *
034 */
035public class LogColorMapper extends ContinuousColorMapperTransform {
036
037        private int base;
038
039        /**
040         * Creates a new LogColorMapper with base 10.
041         * @param logspaceMapper
042         */
043        public LogColorMapper(ContinuousColorMapper logspaceMapper) {
044                this(logspaceMapper, 10);
045        }
046
047        /**
048         * If logspaceMapper maps values x1 to x2, this creates a
049         * mapper for values base^x1 to base^x2
050         *
051         * @param logspaceMapper logspace mapper
052         * @param base The base of the logorithm
053         */
054        public LogColorMapper(ContinuousColorMapper logspaceMapper, int base) {
055                super(logspaceMapper);
056                this.base = base;
057        }
058
059        /**
060         * Apply log transform.
061         * @param value
062         * @return log_b(value)
063         * @see org.biojava.nbio.structure.gui.util.color.ContinuousColorMapperTransform#transform(double)
064         */
065        @Override
066        public double transform(double value) {
067                double logValue = Math.log(value>0?value:0)/Math.log(base);
068                return logValue;
069        }
070
071}