diff --git a/api/graphql.go b/api/graphql.go index e508b95..e45489d 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -409,6 +409,7 @@ func NewSchema(config Config) (graphql.Schema, error) { }, }), Args: graphql.FieldConfigArgument{ + "color": &graphql.ArgumentConfig{Type: graphql.String, Description: "按主色调筛选图像, 使用十六进制颜色代码(#FF1414)"}, "explorer_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中指定收藏夹的"}, "collect_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中指定收藏夹的"}, "collect": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中指定用户收藏过的"}, @@ -627,6 +628,53 @@ func NewSchema(config Config) (graphql.Schema, error) { )) } + // 数据库中筛选: 按图像主色调颜色筛选 + if p.Args["color"] != nil { + // 将 16 进制颜色转换为 RGB 三值 + + // hexToRGB 将十六进制颜色转换为 RGB + hexToRGB := func(hex string) (int, int, int, error) { + // 去掉 # 号 + if strings.HasPrefix(hex, "#") { + hex = hex[1:] + } + + // 检查是否是有效的十六进制颜色 + if len(hex) != 6 { + return 0, 0, 0, fmt.Errorf("invalid hex color") + } + + // 解析红色、绿色、蓝色通道的十六进制值 + r, err := strconv.ParseInt(hex[:2], 16, 0) + if err != nil { + return 0, 0, 0, err + } + g, err := strconv.ParseInt(hex[2:4], 16, 0) + if err != nil { + return 0, 0, 0, err + } + b, err := strconv.ParseInt(hex[4:], 16, 0) + if err != nil { + return 0, 0, 0, err + } + + return int(r), int(g), int(b), nil + } + r, g, b, err := hexToRGB(p.Args["color"].(string)) + if err != nil { + fmt.Println("hexToRGB", err) + return nil, err + } + query = query.Where(goqu.Ex{ + "web_images.color_r > ?": r - 10, + "web_images.color_r < ?": r + 10, + "web_images.color_g > ?": g - 10, + "web_images.color_g < ?": g + 10, + "web_images.color_b > ?": b - 10, + "web_images.color_b < ?": b + 10, + }) + } + // 取所有数据的前N条 sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL()