如何通过 API 获取预设 Google 日历颜色的正确颜色值

Nic*_* H. 5 google-calendar-api

根据文档(参考日历颜色),可以通过属性“backgroundColor”“foregroundColor”和/或“colorId”检索日历颜色。

在我原来的 Google 日历中,我有一个名为“cacao”的日历,当我检查页面时,该日历的颜色为rgb(121, 85, 72) / #795548。但是,当我通过 API 检索该议程的颜色时(直接从 Calendar 对象中获取 backgroundColor 值或通过在 Colors 对象中查找 colorId,它们都匹配),返回的颜色为 rgb( 202, 189, 191) / #cabdbf。我尝试的每种预设颜色都会发生这种不匹配。当我在 Google 日历中选择自定义颜色时,该自定义值通过 API 正确传递。

如何通过 Google Calendar API 获取预设日历颜色的正确颜色值?

编辑 - 在下面添加了代码片段

扩展类客户端:

function getCalendars()
{
    $service = new Google_Service_Calendar($this);
    $calendarList = $service->calendarList->listCalendarList();
    while (true) {
        foreach ($calendarList->getItems() as $calendarListEntry) {
            $calendars[] = $calendarListEntry;
        }
        $pageToken = $calendarList->getNextPageToken();
        if ($pageToken) {
            $optParams = array('pageToken' => $pageToken);
            $calendarList = $service->calendarList->listCalendarList($optParams);
        } else {
            break;
        }
    }
    return $calendars;
}
Run Code Online (Sandbox Code Playgroud)
function getColors()
{
    $service = new Google_Service_Calendar($this);
    $colors = $service->colors->get();
    $col = [];
    foreach ($colors->getCalendar() as $key => $color) {
        $col['calendar'][$key] = $color;
    }
    foreach ($colors->getEvent() as $key => $color) {
        $col['event'][$key] = $color;
    }
    return $col;
}
Run Code Online (Sandbox Code Playgroud)

getCalendars 的结果:

[
    {
        ...
        "backgroundColor": "#16a765",
        "colorId": "8",
        ...
    },
    {
        ...
        "backgroundColor": "#cabdbf",  <-- Agenda color in example
        "colorId": "20",  <-- Agenda color ID in example
        ...
    },
    {
        ...
        "backgroundColor": "#9fc6e7",
        "colorId": "15",
        ...
    },
    {
        ...
        "backgroundColor": "#ffad46",
        "colorId": "6",
        ...
    },
    {
        ...
        "backgroundColor": "#ac725e",
        "colorId": "1",
        ...
    },
    {
        ...
        "backgroundColor": "#fbe983",
        "colorId": "11",
        ...
    }
]
Run Code Online (Sandbox Code Playgroud)

获取颜色的结果:

{
    "calendar": {
        "1": {
            "background": "#ac725e",
            "foreground": "#1d1d1d"
        },
        ...
        "6": {
            "background": "#ffad46",
            "foreground": "#1d1d1d"
        },
        ...
        "8": {
            "background": "#16a765",
            "foreground": "#1d1d1d"
        },
        ...
        "11": {
            "background": "#fbe983",
            "foreground": "#1d1d1d"
        },
        ...
        "15": {
            "background": "#9fc6e7",
            "foreground": "#1d1d1d"
        },
        ...
        "20": {  <-- Agenda color ID in example
            "background": "#cabdbf", <-- Agenda color in example
            "foreground": "#1d1d1d"
        },
        ...
    },
    "event": {
        "1": {
            "background": "#a4bdfc",
            "foreground": "#1d1d1d"
        },
        ...
        "11": {
            "background": "#dc2127",
            "foreground": "#1d1d1d"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:颜色rgb(121, 85, 72) / #795548不是Google 返回完整数组的一部分。

实际的 Google 日历源检查:

Google 日历 HTML 元素检查

Yev*_*ych 3

对于任何仍然想知道的人来说,API 总是返回“经典”颜色。OP 在 UI 中看到的颜色(很可能你也是)是“现代的”。您可以在 UI 中在“经典”和“现代”之间进行更改,但无法 (AFAIC) 使用 API 检索“现代”颜色。我知道,确实是“WTF”。

您可以在Google Calendar Simple API 的文档中查看颜色比较。