You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Cypher在Neo4J中存储并查找最近似颜色?

适配Cypher的最近颜色查询方案

核心思路和原SQL方案一致——通过计算RGB颜色值的欧氏距离,找出距离最小的匹配颜色。

假设数据模型

  • 目标颜色节点:标签TargetColor,包含属性target_r、target_g、target_b(要匹配的RGB值)
  • 颜色库节点:标签Color,包含属性r、g、b、name(参考颜色的RGB值和名称)

查询语句

MATCH (t:TargetColor), (c:Color)
WITH
  c,
  SQRT(
    (t.target_r - c.r)^2 +
    (t.target_g - c.g)^2 +
    (t.target_b - c.b)^2
  ) AS distance
ORDER BY distance ASC
LIMIT 1
RETURN c.name AS nearest_color, c.r, c.g, c.b, distance

扩展说明

  • 如果目标颜色不是节点,而是直接传入的RGB值,可改用参数形式:
WITH $target_r AS tr, $target_g AS tg, $target_b AS tb
MATCH (c:Color)
WITH
  c,
  SQRT(
    (tr - c.r)^2 +
    (tg - c.g)^2 +
    (tb - c.b)^2
  ) AS distance
ORDER BY distance ASC
LIMIT 1
RETURN c.name AS nearest_color, c.r, c.g, c.b, distance
  • 欧氏距离是颜色相似度的基础衡量方法,若需更精准的专业匹配,可替换为CIEDE2000等颜色差值公式,只需对应调整Cypher中的距离计算逻辑即可

内容的提问来源于stack exchange,提问作者Armel Hyacinthe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 22:50:20