如何将convertcolor函数应用于数据中列出的RGB值。

发布于 2025-02-04 17:21:37 字数 2105 浏览 3 评论 0原文

我正在运行一个实验,需要比较从两个不同时间点选择的RGB值的一致性。为此,我需要将RGB转换为XYZ值,我知道我可以使用ConvertColor进行。

然而!我无法找到能够使用ConvertColor将多个存储在数据中的多个RGB值转换为XYZ的有效方法。我有一个问题在于我不知道如何将RGB值存储为数字值而不是字符的事实。

我的data.table命名syn_rgb将被列出如下:

idgraphemergb_1rgb_2
s01a(37,13,219)(39,23,211( 50,3,19
) s01b(40,23,23,11)
S02 S02A(43,57,89)(50,50,78
S02B(48,59,199)(50,63,178)
S03A(100,123,209)(89,112,200)
S03B( 89,120,199)(87,119,175)

rgb_1是第一次给出的RGB,rgb_2第二次给定的RGB。

这是我到目前为止尝试的:

syn_XYZ <- synRGB(,. (s_xyz = convertColor(RGB_1, from = "sRGB", to = "XYZ", clip = TRUE)), by =. (ID, Grapheme)]

在这里,我正在尝试将rgb_1中列出的所有RGB值转换为xyz每个参与者的值在syn_xyz中。

我尝试使用as.numericstrtoi将RGB值从字符转换为数字,但它无法想象。

我对编程仍然相对较新,因此,如果有一种比尝试这样做的要高得多的方法,我将非常感谢,因为这是我想到如何做的唯一方法。

谢谢

I'm running an experiment where I need to compare the consistency of RGB values selected from two different time points. To do this, I need to convert RGB to XYZ values which I know I can do using ConvertColor.

However! I can't figure out an efficient way of being able to convert multiple RGB values stored in data.table into XYZ using ConvertColor. I have a feeling the problem lies in the fact that I can't figure out how to store RGB values as numeric values rather than characters.

My data.table named syn_RGB will be set out as follows:

IDGraphemeRGB_1RGB_2
S01A(37, 13, 219)(39, 23, 211)
S01B(50, 3, 19)(40, 23, 11)
S02A(43, 57, 89)(50, 50, 78)
S02B(48, 59, 199)(50, 63, 178)
S03A(100, 123, 209)(89, 112, 200)
S03B(89, 120, 199)(87, 119, 175)

RGB_1 is the RGB given for the first time, RGB_2 the RGB given the second time.

This is what I've tried so far:

syn_XYZ <- synRGB(,. (s_xyz = convertColor(RGB_1, from = "sRGB", to = "XYZ", clip = TRUE)), by =. (ID, Grapheme)]

Here I'm trying to convert all the RGB values listed in RGB_1 into XYZ values for each participant and each grapheme and store them in syn_XYZ.

I've tried converting the RGB values from characters to numeric using as.numeric and strtoi but it doesn't work as you can imagine.

I'm still relatively new to programming so if there's a far more efficient way than trying to do it like this, I would be very appreciative since this is the only way I can think of how to do it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2025-02-11 17:21:37

我认为data.table对此没有任何“有效”的东西,而是对字符串向量的处理。虽然看起来很笨拙,但它的作用是:

do.call(rgb, c(asplit(do.call(rbind, lapply(regmatches(syn_RGB$RGB_1, gregexpr("\\d+", syn_RGB$RGB_1)), as.integer)), 2), list(maxColorValue = 255)))
# [1] "#250DDB" "#320313" "#2B3959" "#303BC7" "#647BD1" "#5978C7"

漫步,大部分是内而外:

  • regMatches(。gregexpr(“ \\ d+”,。))将看起来像数字的东西提取到单个数字中;即,“(37,13,219)”转换为c(“ 37”,“ 13”,“ 219”);这将返回列表,其长度与nrow(Syn_rgb)
  • lapply(。,as.integer)将字符串列表转换为列表整数例如,列表(C(“ 37”,“ 13”,“ 219”),C(“ 52”,“ 3”,“ 19”),...) (C(37,13,219),C(52,3,19),...)
  • 我们从(有效地)“ R,G,B”值的列表开始,但是为了将其与另一个函数一起使用,将在各自的向量中使用所有RS,所有GS和所有BS都有帮助;为此,do.call(rbind,。)将向量列表转换为矩阵,然后asplit(。,2)将其转换为列列表根据我们的需要。
  • do.call(rgb,。)rgb函数(一次)调用asplit(。,2)的三个值中的每个值列表作为其参数,添加list的参数(MaxColorValue = 255)(由于RGB通常希望其值在[0,1] )。

I don't think data.table has anything "efficient" for this, it's just a handling of a string vector. As clunky as it looks, this works:

do.call(rgb, c(asplit(do.call(rbind, lapply(regmatches(syn_RGB$RGB_1, gregexpr("\\d+", syn_RGB$RGB_1)), as.integer)), 2), list(maxColorValue = 255)))
# [1] "#250DDB" "#320313" "#2B3959" "#303BC7" "#647BD1" "#5978C7"

Walk through, mostly inside-out:

  • regmatches(., gregexpr("\\d+", .)) extracts things that look like numbers into individual numbers; i.e., "(37, 13, 219)" is converted into c("37", "13", "219"); this returns a list with the same length as nrow(syn_RGB)
  • lapply(., as.integer) converts the list of strings into a list of integer; e.g., list(c("37", "13", "219"), c("52", "3", "19"), ...) into list(c(37, 13, 219), c(52, 3, 19), ...).
  • We start with (effectively) a list of "R, G, B" values, but to programmatically use that with another function, it would help to have a list with all Rs, all Gs, and all Bs in respective vectors; to do that, do.call(rbind, .) converts that list of vectors into a matrix, and then asplit(., 2) converts it to a list of columns as we need.
  • do.call(rgb, .) calls the rgb function (once) with each of the three values of the asplit(., 2) list as its arguments, adding the argument of list(maxColorValue=255) (since rgb normally wants its values to be on [0,1]).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文