'How to get correct color value for preset Google Calendar colors via API
According to the docs (ref calendar and color) the calendar colors can be retrieved via the properties "backgroundColor", "foregroundColor" and/or "colorId".
In my original Google Calendar I have a calendar with color called 'cacao' and when I inspect the page the color of this calendar is rgb(121, 85, 72) / #795548. However when I retrieve the colors for that agenda through the API (either directly the backgroundColor value from the Calendar object or via looking up the colorId in the Colors object, they both match) the returned color is rgb(202, 189, 191) / #cabdbf. This mismatch occurs for every preset color I try. When I pick a custom color in Google Calendar this custom value is passed correctly via the API.
How do I get the correct color values for preset calendar colors via the Google Calendar API?
Edit - Added code snippet below
Extending class Client:
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;
}
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;
}
Result of 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",
...
}
]
Result of getColors:
{
"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"
}
}
}
Note: Color rgb(121, 85, 72) / #795548 is not part of the complete array returned by Google.
Actual Google Calendar source inspect:
Solution 1:[1]
I'm currently trying to wrap my head around this problem too ..! All the preset colors returned by Google seems to be altered from the ones we can observe via Google Calendar. Did you find a solution yet ?
The modifying pattern of the color also seems to be random, here are some examples :
- #e67c73 -> #d06b64
- #8e24aa -> #cd74e6
- #4285f4 -> #9fc6e7
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Valentin Py |