RGB到十六进制转换器

时间:2015-07-22 21:40:43

标签: r

假设我有这个载体

x <- c("165 239 210", "111 45 93")

是否有一个整齐的包将RGB值转换为R中的十六进制值?我找到了许多javascript方式,但没有一个用于R。

x <- "#A5EFD2" "#6F2D5D"

4 个答案:

答案 0 :(得分:20)

只需将字符串拆分,然后使用rgb

x <- c("165 239 210", "111 45 93")
sapply(strsplit(x, " "), function(x)
    rgb(x[1], x[2], x[3], maxColorValue=255))
#[1] "#A5EFD2" "#6F2D5D"

答案 1 :(得分:0)

您可以转换为数字矩阵并使用NSArray *numbers; - (int)sumWithCondition:(NSString *)condition { int sum = 0; int modCondition = 0; if ([condition isEqualToString:@"odd"]) { modCondition = 1; //Set Mod condition to odd } else { modCondition = 0; //Set Mod condition to even } for (int i = 0; i < [numbers count]; i++){ //Iterate over each value in array int thisValue = [[numbers objectAtIndex:i] intValue]; if ((thisValue % 2) == modCondition) { //If value is odd or even depending on condition sum += thisValue; //Then add value to sum } } return sum; }

colourvalues::convert_colours()

答案 2 :(得分:0)

此答案基于 answer to this same question by Hong Ooi,但提供了一个 函数,该函数将 col2rgb() 返回的形式的 rgb 值矩阵作为输入。这意味着我们可以使用这两个函数从十六进制转换为 rgb,然后再转换回来。

从窗体的 RGB 颜色矩阵开始

      [,1] [,2]
red    213    0
green   94  158
blue     0  115

此函数将矩阵转换为十六进制颜色向量。

rgb2col = function(rgbmat){
  ProcessColumn = function(col){
    rgb(rgbmat[1, col], 
        rgbmat[2, col], 
        rgbmat[3, col], 
        maxColorValue = 255)
  }
  sapply(1:ncol(rgbmat), ProcessColumn)
}

用例:您可能想要手动修改调色板,但对使用十六进制数字感到不舒服。例如,假设我想使两种颜色的向量变暗一点。

# Colors to darken
ColorsHex = c("#D55E00","#009E73")

# Convert to rgb
# This is the step where we get the matrix
ColorsRGB = col2rgb(ColorsHex)

# Darken colors by lowering values of RGB
ColorsRGBDark = round(ColorsRGB*.7)

# Convert back to hex
ColorsHexDark = rgb2col(ColorsRGBDark)

此解决方案的主要优点是 rgb2col(col2rgb(x)) 等于 x(或 col2rgb(rgb2col(x)) 等于 x)。

答案 3 :(得分:-2)

您可以使用R中的String[] letters = {"s", "a"}; String randomSad = (letters[new Random().nextInt(letters.length)]); tv1 = (TextView) findViewById(R.id.textview11); tv1.setText(randomSad); 函数和以下帖子中的提示:How to display hexadecimal numbers in C?

相关问题