1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received A copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
################################################################################
# FUNCTION: DESCRIPTION:
# .asRGB Converts any R color to RGB (red/green/blue)
# .chcode Changes from one to another number system
# .hex.to.dec Converts heximal numbers do decimal numbers
# .dec.to.hex Converts decimal numbers do heximal numbers
################################################################################
.asRGB <-
function(col = rainbowPalette(64), alpha = FALSE)
{
# A function implemented by Diethelm Wuertz
# Description:
# Converts any R color to RGB (red/green/blue)
# Arguments:
# col - vector of any of the three kind of R colors, i.e., either a
# color name (an element of colors()), a hexadecimal string of
# the form "#rrggbb", or an integer i meaning palette()[i].
# alpha - a logical value indicating whether alpha channel values
# should be returned.
# FUNCTION:
# Color Conversion:
result <- col2rgb(col = col, alpha = alpha)
# Return Value:
t(result)
}
# ------------------------------------------------------------------------------
.chcode <-
function(b, base.in = 2, base.out = 10, digits = "0123456789ABCDEF")
{
# A function implemented by Diethelm Wuertz
# Description:
# Changes from one to another number system
# Arguments:
# b - number specified in the input base
# b.in - input base
# b.out - output base
# digits - digits string
# Value:
# returns the input in the form represented by the output base
# Author:
# Peter Wolf Universitaet Bielefeld
# from: http://tolstoy.newcastle.edu.au/R/help/05/04/2085.html
# FUNCTION:
# Change Number System:
digits = substring(digits,1:nchar(digits),1:nchar(digits))
if (length(base.in) == 1)
base.in = rep(base.in, max(nchar(b) - 1))
if (is.numeric(b))
b = as.character(as.integer(b))
b.num = lapply(strsplit(b, ""),
function(x) {match(x, digits)-1} )
result = lapply(b.num,
function(x) {cumprod(rev(c(base.in,1))[1:length(x)]) %*% rev(x)} )
number = unlist(result)
# DW Print Output Suppressed
# cat("decimal representation:",number,"\n")
if (length(base.out) == 1) {
base.out<-rep(base.out, 1+ceiling(log(max(number), base = base.out)))
}
n.base = length(base.out)
result = NULL
for(i in n.base:1){
result = rbind(number %% base.out[i], result)
number = floor(number/base.out[i])
}
result[]<-digits[result+1]
ans = apply(result, 2, paste, collapse = "")
# Return Value:
ans
}
# ------------------------------------------------------------------------------
.hex.to.dec <-
function(b)
{
# A function implemented by Diethelm Wuertz
# Description:
# Converts heximal numbers do decimal numbers
# Arguments:
# b - a heximal number
# Value:
# returns a heximal numbers as decimal numbers
# FUNCTION:
# Hex to Bin:
ans = as.numeric(.chcode(b, base.in = 16, base.out = 10))
# Return Value:
ans
}
# ------------------------------------------------------------------------------
.dec.to.hex <-
function(b)
{
# A function implemented by Diethelm Wuertz
# Description:
# Converts decimal numbers do heximal numbers
# Arguments:
# x - a decimal number
# Value:
# returns a decimal numbers as heximal numbers
# FUNCTION:
# Decimal to Hex:
ans = .chcode(b, base.in = 10, base.out = 16)
# Return Value:
ans
}
################################################################################
|