集成颜色字段

This commit is contained in:
2024-11-14 16:19:14 +08:00
parent 4785fb9f8f
commit 3229e70023
2 changed files with 36 additions and 18 deletions

View File

@@ -13,9 +13,15 @@
# OCR
- [ ] 脚本统计各关键词总量
- [ ] 筛选条件支持多选 颜色,风格 | 类型,主题,功能,材质图案(多选逗号分隔)
- [ ] 按颜色筛选, 周期性脚本自动补全三色
ALTER TABLE web_images ADD COLUMN color_0_r TINYINT UNSIGNED;
ALTER TABLE web_images ADD COLUMN color_0_g TINYINT UNSIGNED;
ALTER TABLE web_images ADD COLUMN color_0_b TINYINT UNSIGNED;
ALTER TABLE web_images ADD COLUMN color_1_r TINYINT UNSIGNED;
ALTER TABLE web_images ADD COLUMN color_1_g TINYINT UNSIGNED;
ALTER TABLE web_images ADD COLUMN color_1_b TINYINT UNSIGNED;
```bash

View File

@@ -104,6 +104,19 @@ func NewSchema(config Config) (graphql.Schema, error) {
log.Fatalln("连接数据库失败", err)
}
//// 定时检查补全颜色字段
//checkColorNullRows := func() {
// for {
// var ids struct {
// ID int
// }
// if err := db.Table("web_images").Where("color IS NULL").Scan(ids).Error; err != nil {
// fmt.Println("定时检查补全颜色字段查询失败", err)
// }
// fmt.Println("定时检查补全颜色字段查询成功", ids)
// }
//}
// 用户的可选字段
user := graphql.NewObject(graphql.ObjectConfig{
Name: "User",
@@ -630,20 +643,16 @@ 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 {
@@ -657,23 +666,26 @@ func NewSchema(config Config) (graphql.Schema, error) {
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))
// 逗号分割且去除空格
colors := strings.Split(strings.ReplaceAll(p.Args["color"].(string), " ", ""), ",")
for index, color := range colors {
r, g, b, err := hexToRGB(color)
if err != nil {
fmt.Println("hexToRGB", err)
fmt.Println("hexToRGB", index, 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,
fmt.Sprintf("web_images.color_%d_r > ?", index): r - 10,
fmt.Sprintf("web_images.color_%d_r < ?", index): r + 10,
fmt.Sprintf("web_images.color_%d_g > ?", index): g - 10,
fmt.Sprintf("web_images.color_%d_g < ?", index): g + 10,
fmt.Sprintf("web_images.color_%d_b > ?", index): b - 10,
fmt.Sprintf("web_images.color_%d_b < ?", index): b + 10,
})
}
}
// 取所有数据的前N条
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL()