转换颜色格式并计算 WCAG 对比度

GET/color
公开访问curl路径兼容

_format=svg 返回原色、更亮、更暗和补色的色板,公开缓存 1 天。对比度按不透明 RGB 计算。 访问类别:匿名 API,可携带 Token 使用独立额度

请求参数

hexquery必填
3、4、6 或 8 位十六进制,可带 #
string · 3、4、6 或 8 位十六进制,可带 # · 最短:3 字符 · 最长:9 字符
againstquery
可选的对照色
string · 可选的对照色 · 最短:3 字符 · 最长:9 字符
_formatquery
响应格式为 svg、json 或 text。/color 默认 JSON,带 curl* 标记的接口默认 SVG。plain 是 text 的别名。未识别的值继续按 Accept 协商。
string · 可选:svg、json、text
_colorquery
控制 text 响应的 ANSI 颜色。0、false、off、no 关闭;省略或其他值保留。
string · 可选:0、false、off、no

路径兼容入口

GET /color/{hex}

将 hex 的值进行 URL 编码后填入路径。

响应

application/json

{
  "type": "object",
  "properties": {
    "hex": {
      "type": "string"
    },
    "rgb": {
      "type": "object",
      "properties": {
        "r": {
          "type": "number"
        },
        "g": {
          "type": "number"
        },
        "b": {
          "type": "number"
        },
        "a": {
          "type": "number"
        }
      },
      "required": [
        "r",
        "g",
        "b",
        "a"
      ]
    },
    "hsl": {
      "type": "object",
      "properties": {
        "h": {
          "type": "number"
        },
        "s": {
          "type": "number"
        },
        "l": {
          "type": "number"
        }
      },
      "required": [
        "h",
        "s",
        "l"
      ]
    },
    "css": {
      "type": "object",
      "properties": {
        "hex": {
          "type": "string"
        },
        "rgb": {
          "type": "string"
        },
        "hsl": {
          "type": "string"
        }
      },
      "required": [
        "hex",
        "rgb",
        "hsl"
      ]
    },
    "luminance": {
      "type": "number"
    },
    "contrast": {
      "type": "object",
      "properties": {
        "black": {
          "type": "object",
          "properties": {
            "ratio": {
              "type": "number"
            },
            "aa": {
              "type": "boolean"
            },
            "aaa": {
              "type": "boolean"
            },
            "aaLarge": {
              "type": "boolean"
            },
            "aaaLarge": {
              "type": "boolean"
            }
          },
          "required": [
            "ratio",
            "aa",
            "aaa",
            "aaLarge",
            "aaaLarge"
          ]
        },
        "white": {
          "type": "object",
          "properties": {
            "ratio": {
              "type": "number"
            },
            "aa": {
              "type": "boolean"
            },
            "aaa": {
              "type": "boolean"
            },
            "aaLarge": {
              "type": "boolean"
            },
            "aaaLarge": {
              "type": "boolean"
            }
          },
          "required": [
            "ratio",
            "aa",
            "aaa",
            "aaLarge",
            "aaaLarge"
          ]
        },
        "against": {
          "type": [
            "object",
            "null"
          ],
          "properties": {
            "ratio": {
              "type": "number"
            },
            "aa": {
              "type": "boolean"
            },
            "aaa": {
              "type": "boolean"
            },
            "aaLarge": {
              "type": "boolean"
            },
            "aaaLarge": {
              "type": "boolean"
            }
          },
          "required": [
            "ratio",
            "aa",
            "aaa",
            "aaLarge",
            "aaaLarge"
          ]
        }
      },
      "required": [
        "black",
        "white",
        "against"
      ]
    }
  },
  "required": [
    "hex",
    "rgb",
    "hsl",
    "css",
    "luminance",
    "contrast"
  ],
  "additionalProperties": {}
}

image/svg+xml

{
  "type": "string"
}

响应示例

成功响应

curl -sS 'https://oit.sh/color/336699?_format=json'
HTTP 200
content-type: application/json
content-encoding: gzip
cache-control: no-store

{
  "hex": "#336699",
  "rgb": {
    "r": 51,
    "g": 102,
    "b": 153,
    "a": 1
  },
  "hsl": {
    "h": 210,
    "s": 50,
    "l": 40
  },
  "css": {
    "hex": "#336699",
    "rgb": "rgb(51, 102, 153)",
    "hsl": "hsl(210, 50%, 40%)"
  },
  "luminance": 0.125,
  "contrast": {
    "black": {
      "ratio": 3.5,
      "aa": false,
      "aaa": false,
      "aaLarge": true,
      "aaaLarge": false
    },
    "white": {
      "ratio": 6,
      "aa": true,
      "aaa": false,
      "aaLarge": true,
      "aaaLarge": true
    },
    "against": null
  }
}

错误示例

curl -sS 'https://oit.sh/color?hex=invalid+value&against=hello&_format=json'
HTTP 400
content-type: application/json
content-encoding: gzip
cache-control: no-store

{
  "error": {
    "code": "INVALID_INPUT",
    "message": "参数无效",
    "issues": [
      {
        "location": "query",
        "field": "hex",
        "code": "too_big",
        "message": "字符数最多为 9"
      }
    ]
  }
}

curl 访问示例

curl、wget、HTTPie 默认得到 text/plain。示例展示纯文本输出。

命令与输出

curl -sS 'https://oit.sh/color/336699?_color=0'
HTTP 200
content-type: text/plain; charset=utf-8
content-encoding: gzip
cache-control: no-store

hex: #336699
rgb:
  r: 51
  g: 102
  b: 153
  a: 1
hsl:
  h: 210
  s: 50
  l: 40
css:
  hex: #336699
  rgb: rgb(51, 102, 153)
  hsl: hsl(210, 50%, 40%)
luminance: 0.125
contrast:
  black:
    ratio: 3.5
    aa: false
    aaa: false
    aaLarge: true
    aaaLarge: false
  white:
    ratio: 6
    aa: true
    aaa: false
    aaLarge: true
    aaaLarge: true
  against: null

在线调试

必填
Ctrl / ⌘ + Enter
curl -X GET 'https://oit.sh/color?hex=336699'

响应

等待请求

等待发送请求