getActivity(); if (null !== $activity && $activity->hasColor()) { return $activity->getColor(); } $project = $timesheet->getProject(); if (null !== $project) { if ($project->hasColor()) { return $project->getColor(); } $customer = $project->getCustomer(); if ($customer->hasColor()) { return $customer->getColor(); } } return Constants::DEFAULT_COLOR; } public function getColor(EntityWithMetaFields $entity, bool $defaultColor = false): ?string { if ($entity instanceof Timesheet) { $color = $this->getTimesheetColor($entity); if ($color === Constants::DEFAULT_COLOR && !$defaultColor) { $color = null; } return $color; } if ($entity instanceof Activity) { if ($entity->hasColor()) { return $entity->getColor(); } if (null !== $entity->getProject()) { $entity = $entity->getProject(); } } if ($entity instanceof Project) { if ($entity->hasColor()) { return $entity->getColor(); } $entity = $entity->getCustomer(); } if ($entity instanceof Customer) { if ($entity->hasColor()) { return $entity->getColor(); } } return $defaultColor ? Constants::DEFAULT_COLOR : null; } public function getRandom(?string $input = null): string { if ($input === null) { return $this->getRandomColor(); } return $this->getRandomFromPalette($input); } public function getRandomColor(): string { return \sprintf('#%06x', rand(0, 16777215)); } public function getRandomFromPalette(string $input): string { $id = 0; for ($pos = 0; $pos < \strlen($input); $pos++) { $id += mb_ord($input[$pos], 'UTF-8'); } $key = $id % \count(self::PALETTE); return self::PALETTE[$key]; } public function getFontContrastColor(string $color): string { if (empty($color) || $color[0] !== '#') { // do not throw exception on invalid colors, as they were not validated in the past $color = Constants::DEFAULT_COLOR; } $color = substr($color, 1); $length = \strlen($color); if ($length === 3) { $color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2]; } elseif ($length !== 6) { $color = substr(Constants::DEFAULT_COLOR, 1); } $r = hexdec(substr($color, 0, 2)); $g = hexdec(substr($color, 2, 2)); $b = hexdec(substr($color, 4, 2)); $yiq = (($r * 299) + ($g * 587) + ($b * 114)) / 1000; return ($yiq >= 128) ? '#000000' : '#ffffff'; } }