From 23fd9ae7316835e66acbc999f2e63ac40e44f414 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 16 Jun 2022 06:30:59 +0200 Subject: [PATCH] Colors: Use int16 instead of int8 to avoid (de)serialization issues --- internal/entity/file.go | 4 ++-- internal/entity/file_json.go | 2 +- internal/entity/photo.go | 2 +- internal/form/search_photos.go | 2 +- internal/form/search_photos_test.go | 2 +- internal/form/serialize_test.go | 2 +- internal/search/photos_results.go | 4 ++-- pkg/colors/chroma.go | 6 +++--- pkg/colors/chroma_test.go | 4 ++-- pkg/colors/colors.go | 6 +++--- pkg/colors/colors_test.go | 4 ++-- pkg/colors/luminance.go | 2 +- 12 files changed, 20 insertions(+), 20 deletions(-) diff --git a/internal/entity/file.go b/internal/entity/file.go index f39ca6a0d..d51e5b2c8 100644 --- a/internal/entity/file.go +++ b/internal/entity/file.go @@ -81,7 +81,7 @@ type File struct { FileColors string `gorm:"type:VARBINARY(9);" json:"Colors" yaml:"Colors,omitempty"` FileLuminance string `gorm:"type:VARBINARY(9);" json:"Luminance" yaml:"Luminance,omitempty"` FileDiff int `json:"Diff" yaml:"Diff,omitempty"` - FileChroma int8 `json:"Chroma" yaml:"Chroma,omitempty"` + FileChroma int16 `json:"Chroma" yaml:"Chroma,omitempty"` FileSoftware string `gorm:"type:VARCHAR(64)" json:"Software" yaml:"Software,omitempty"` FileError string `gorm:"type:VARBINARY(512)" json:"Error" yaml:"Error,omitempty"` ModTime int64 `json:"ModTime" yaml:"-"` @@ -167,7 +167,7 @@ type FileInfos struct { FileColors string FileLuminance string FileDiff int - FileChroma int8 + FileChroma int16 } // FirstFileByHash gets a file in db from its hash diff --git a/internal/entity/file_json.go b/internal/entity/file_json.go index eac5cf672..d60522d12 100644 --- a/internal/entity/file_json.go +++ b/internal/entity/file_json.go @@ -41,7 +41,7 @@ func (m *File) MarshalJSON() ([]byte, error) { Colors string `json:",omitempty"` Luminance string `json:",omitempty"` Diff int `json:",omitempty"` - Chroma int8 `json:",omitempty"` + Chroma int16 `json:",omitempty"` HDR bool `json:",omitempty"` Watermark bool `json:",omitempty"` Software string `json:",omitempty"` diff --git a/internal/entity/photo.go b/internal/entity/photo.go index ed87a4f02..a3df54f94 100644 --- a/internal/entity/photo.go +++ b/internal/entity/photo.go @@ -85,7 +85,7 @@ type Photo struct { PhotoQuality int `gorm:"type:SMALLINT" json:"Quality" yaml:"Quality,omitempty"` PhotoFaces int `json:"Faces,omitempty" yaml:"Faces,omitempty"` PhotoResolution int `gorm:"type:SMALLINT" json:"Resolution" yaml:"-"` - PhotoColor int8 `json:"Color" yaml:"-"` + PhotoColor int16 `json:"Color" yaml:"-"` CameraID uint `gorm:"index:idx_photos_camera_lens;default:1" json:"CameraID" yaml:"-"` CameraSerial string `gorm:"type:VARBINARY(160);" json:"CameraSerial" yaml:"CameraSerial,omitempty"` CameraSrc string `gorm:"type:VARBINARY(8);" json:"CameraSrc" yaml:"-"` diff --git a/internal/form/search_photos.go b/internal/form/search_photos.go index febebc365..69970f23b 100644 --- a/internal/form/search_photos.go +++ b/internal/form/search_photos.go @@ -44,7 +44,7 @@ type SearchPhotos struct { Dist uint `form:"dist" example:"dist:5" notes:"Distance in km in combination with lat/lng"` Fmin float32 `form:"fmin" notes:"F-number (min)"` Fmax float32 `form:"fmax" notes:"F-number (max)"` - Chroma uint8 `form:"chroma" example:"chroma:70" notes:"Chroma (0-100)"` + Chroma int16 `form:"chroma" example:"chroma:70" notes:"Chroma (0-100)"` Diff uint32 `form:"diff" notes:"Differential Perceptual Hash (000000-FFFFFF)"` Mono bool `form:"mono" notes:"Finds pictures with few or no colors"` Geo bool `form:"geo" notes:"Finds pictures with GPS location"` diff --git a/internal/form/search_photos_test.go b/internal/form/search_photos_test.go index 3c25e5ffe..6452b91ba 100644 --- a/internal/form/search_photos_test.go +++ b/internal/form/search_photos_test.go @@ -121,7 +121,7 @@ func TestParseQueryString(t *testing.T) { t.Fatal("err should be nil") } - assert.Equal(t, uint8(200), form.Chroma) + assert.Equal(t, int16(200), form.Chroma) assert.Equal(t, "te:st", form.Title) assert.Equal(t, time.Date(2018, 01, 15, 0, 0, 0, 0, time.UTC), form.After) assert.Equal(t, float32(33.45343), form.Lng) diff --git a/internal/form/serialize_test.go b/internal/form/serialize_test.go index 722ebdcdd..e9ecb76c6 100644 --- a/internal/form/serialize_test.go +++ b/internal/form/serialize_test.go @@ -25,7 +25,7 @@ type TestForm struct { Dist uint `form:"dist"` Fmin float32 `form:"fmin"` Fmax float32 `form:"fmax"` - Chroma uint8 `form:"chroma"` + Chroma int16 `form:"chroma"` Diff uint32 `form:"diff"` Mono bool `form:"mono"` Portrait bool `form:"portrait"` diff --git a/internal/search/photos_results.go b/internal/search/photos_results.go index aaab155f6..f320cd8b8 100644 --- a/internal/search/photos_results.go +++ b/internal/search/photos_results.go @@ -42,7 +42,7 @@ type Photo struct { PhotoFaces int `json:"Faces,omitempty" select:"photos.photo_faces"` PhotoQuality int `json:"Quality" select:"photos.photo_quality"` PhotoResolution int `json:"Resolution" select:"photos.photo_resolution"` - PhotoColor uint8 `json:"Color" select:"photos.photo_color"` + PhotoColor int16 `json:"Color" select:"photos.photo_color"` PhotoScan bool `json:"Scan" select:"photos.photo_scan"` PhotoPanorama bool `json:"Panorama" select:"photos.photo_panorama"` CameraID uint `json:"CameraID" select:"photos.camera_id"` // Camera @@ -90,7 +90,7 @@ type Photo struct { FileAspectRatio float32 `json:"-" select:"files.file_aspect_ratio"` FileColors string `json:"-" select:"files.file_colors"` FileDiff int `json:"-" select:"files.file_diff"` - FileChroma int8 `json:"-" select:"files.file_chroma"` + FileChroma int16 `json:"-" select:"files.file_chroma"` FileLuminance string `json:"-" select:"files.file_luminance"` Merged bool `json:"Merged" select:"-"` CreatedAt time.Time `json:"CreatedAt" select:"photos.created_at"` diff --git a/pkg/colors/chroma.go b/pkg/colors/chroma.go index e5899999c..9d6b0827b 100644 --- a/pkg/colors/chroma.go +++ b/pkg/colors/chroma.go @@ -3,17 +3,17 @@ package colors import "fmt" // Chroma represents colorfulness. -type Chroma int8 +type Chroma int16 // Percent returns the colourfulness in percent. -func (c Chroma) Percent() int8 { +func (c Chroma) Percent() int16 { if c > 100 { return 100 } else if c < 0 { return 0 } - return int8(c) + return int16(c) } // Hex returns the colourfulness in percent has hex string. diff --git a/pkg/colors/chroma_test.go b/pkg/colors/chroma_test.go index 018cdd484..6c952d64f 100644 --- a/pkg/colors/chroma_test.go +++ b/pkg/colors/chroma_test.go @@ -12,11 +12,11 @@ func TestChroma_Percent(t *testing.T) { t.Run("chroma 15", func(t *testing.T) { perception := ColorPerception{Colors: Colors{Orange, Lime, Cyan}, MainColor: Cyan, Luminance: lMap, Chroma: 15} - assert.Equal(t, int8(15), perception.Chroma.Percent()) + assert.Equal(t, int16(15), perception.Chroma.Percent()) }) t.Run("chroma 127", func(t *testing.T) { perception := ColorPerception{Colors: Colors{Orange, Lime, Cyan}, MainColor: Cyan, Luminance: lMap, Chroma: 127} - assert.Equal(t, int8(100), perception.Chroma.Percent()) + assert.Equal(t, int16(100), perception.Chroma.Percent()) }) } diff --git a/pkg/colors/colors.go b/pkg/colors/colors.go index a7b7b4976..e34d697a2 100644 --- a/pkg/colors/colors.go +++ b/pkg/colors/colors.go @@ -32,7 +32,7 @@ import ( "github.com/photoprism/photoprism/pkg/txt" ) -type Color int8 +type Color int16 type Colors []Color const ( @@ -115,8 +115,8 @@ func (c Color) Name() string { return Names[c] } -func (c Color) ID() int8 { - return int8(c) +func (c Color) ID() int16 { + return int16(c) } func (c Color) Hex() string { diff --git a/pkg/colors/colors_test.go b/pkg/colors/colors_test.go index b8db2f2cf..ed6693c94 100644 --- a/pkg/colors/colors_test.go +++ b/pkg/colors/colors_test.go @@ -28,6 +28,6 @@ func TestColors_Hex(t *testing.T) { assert.Equal(t, "DA0", result) } -func TestColor_Uint8(t *testing.T) { - assert.Equal(t, int8(7), Cyan.ID()) +func TestColor_ID(t *testing.T) { + assert.Equal(t, int16(7), Cyan.ID()) } diff --git a/pkg/colors/luminance.go b/pkg/colors/luminance.go index 6b504fd9b..cb8190b91 100644 --- a/pkg/colors/luminance.go +++ b/pkg/colors/luminance.go @@ -2,7 +2,7 @@ package colors import "fmt" -type Luminance uint8 +type Luminance int16 func (l Luminance) Hex() string { return fmt.Sprintf("%X", l)