> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Similar RGB Color Python Solution with Tests

> Tested Python solution for LeetCode 800 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 800, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Enumeration](/catalog/topics/enumeration). [View on LeetCode](https://leetcode.com/problems/similar-rgb-color/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 800   # by problem number
lcpy gen -s similar_rgb_color   # by problem name
```

## Problem

The red-green-blue color `"#AABBCC"` can be written as `"#ABC"` in shorthand.

* For example, `"#15c"` is shorthand for the color `"#1155cc"`.

The similarity between the two colors `"#ABCDEF"` and `"#UVWXYZ"` is `-(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2`.

Given a string `color` that follows the format `"#ABCDEF"`, return a string represents the color that is most similar to the given color and has a shorthand (i.e., it can be represented as some `"#XYZ"`).

**Any answer** which has the same highest similarity as the best answer will be accepted.

### Examples

```
Input: color = "#09f166"
Output: "#11ee66"
Explanation:
The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73.
This is the highest among any shorthand color.
```

```
Input: color = "#4e3fe1"
Output: "#5544dd"
```

### Constraints

* `color.length == 7`
* `color[0] == '#'`
* `color[i]` is either digit or character in the range `['a', 'f']` for `i > 0`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/similar_rgb_color/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/similar_rgb_color/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1)
    # Space: O(1)
    def similar_rgb(self, color: str) -> str:
        def nearest(channel: str) -> str:
            val = int(channel, 16)
            q = val // 17 + (1 if val % 17 > 8 else 0)
            return f"{17 * q:02x}"

        return "#" + "".join(nearest(color[i : i + 2]) for i in (1, 3, 5))
```

## Complexity

| Time | Space |
| - | - |
| O(1) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.