Skip to content

Commit

Permalink
Output color categories
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Apr 9, 2022
1 parent cb9821c commit 3ba0e55
Show file tree
Hide file tree
Showing 18 changed files with 2,273 additions and 13 deletions.
81 changes: 81 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"

colorful "github.com/lucasb-eyer/go-colorful"
"github.com/mgmeyers/unipdf/v3/core"
"github.com/mgmeyers/unipdf/v3/model"
)
Expand Down Expand Up @@ -55,3 +56,83 @@ func getColor(annotation *model.PdfAnnotation) string {

return ""
}

func getColorCategory(annotation *model.PdfAnnotation) string {
if annotation == nil {
return ""
}

ctx := annotation.GetContext()
annotType := getType(ctx)

switch annotType {
case Highlight:
return pdfObjToColorCategory(ctx.(*model.PdfAnnotationHighlight).C)
case Strike:
return pdfObjToColorCategory(ctx.(*model.PdfAnnotationStrikeOut).C)
case Underline:
return pdfObjToColorCategory(ctx.(*model.PdfAnnotationUnderline).C)
case Rectangle:
return pdfObjToColorCategory(ctx.(*model.PdfAnnotationSquare).C)
case Text:
return pdfObjToColorCategory(ctx.(*model.PdfAnnotationText).C)
}

return ""
}

func pdfObjToColorCategory(c core.PdfObject) string {
if c == nil {
return ""
}

clr, err := c.(*core.PdfObjectArray).ToFloat64Array()
endIfErr(err)

if len(clr) < 3 {
return ""
}

color := colorful.Color{
R: clr[0],
G: clr[1],
B: clr[2],
}
h, s, l := color.Hsl()

// define color category based on HSL
if l < 0.12 {
return "Black"
}
if l > 0.98 {
return "White"
}
if s < 0.2 {
return "Gray"
}
if h < 15 {
return "Red"
}
if h < 45 {
return "Orange"
}
if h < 65 {
return "Yellow"
}
if h < 170 {
return "Green"
}
if h < 190 {
return "Cyan"
}
if h < 263 {
return "Blue"
}
if h < 280 {
return "Purple"
}
if h < 335 {
return "Magenta"
}
return "Red"
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ require (
github.com/gen2brain/go-fitz v1.18.0
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551
github.com/mgmeyers/unipdf/v3 v3.9.0
github.com/lucasb-eyer/go-colorful v1.2.0
)

require (
github.com/softlandia/cpd v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/softlandia/cpd v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/unidoc/unitype v0.2.0 // indirect
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mgmeyers/unipdf/v3 v3.9.0 h1:sMeEIjjYNe+0bf5aITf+3P3rXbnHkWu79LUGVLbIO9g=
github.com/mgmeyers/unipdf/v3 v3.9.0/go.mod h1:ZIbI0M9u3SfhUnF0wR2YGVWlebIqwCikx/sbPlVZSG0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -32,6 +34,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/softlandia/cpd v1.0.0/go.mod h1:LcHy4FZYolw1FpyNAiwI1dblei1AGBBF2FpD1+L9Iqg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
11 changes: 6 additions & 5 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func handleImageAnnot(
}

builtAnnot := &Annotation{
Color: getColor(annotation),
Comment: comment,
ImagePath: imagePath,
Type: Image,
Page: pageIndex + 1,
Color: getColor(annotation),
ColorCategory: getColorCategory(annotation),
Comment: comment,
ImagePath: imagePath,
Type: Image,
Page: pageIndex + 1,
}

date := getDate(annotation)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
type Annotation struct {
AnnotatedText string `json:"annotatedText,omitempty"`
Color string `json:"color,omitempty"`
ColorCategory string `json:"colorCategory,omitempty"`
Comment string `json:"comment,omitempty"`
Date string `json:"date,omitempty"`
ImagePath string `json:"imagePath,omitempty"`
Expand Down Expand Up @@ -228,6 +229,7 @@ func processAnnotations(
builtAnnot := &Annotation{
AnnotatedText: str,
Color: getColor(annotation),
ColorCategory: getColorCategory(annotation),
Comment: comment,
Type: annotType,
Page: pageIndex + 1,
Expand Down
101 changes: 101 additions & 0 deletions vendor/github.com/lucasb-eyer/go-colorful/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions vendor/github.com/lucasb-eyer/go-colorful/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/lucasb-eyer/go-colorful/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3ba0e55

Please sign in to comment.