diff --git a/document.go b/document.go index 3b09f2f..4c1ab8b 100644 --- a/document.go +++ b/document.go @@ -10,12 +10,6 @@ import ( "golang.org/x/image/math/fixed" ) -type TableBlock struct { - Title string - Header []string - Rows []any -} - type Document struct { Blocks []TableBlock } diff --git a/image_table.go b/image_table.go index 5e9999b..a5adb9b 100644 --- a/image_table.go +++ b/image_table.go @@ -181,10 +181,19 @@ func DrawTableWarm(header []string, rows []any) image.Image { y := headerHeight for rowIndex, row := range rows { bg := rowBg1 + if rowIndex%2 == 1 { bg = rowBg2 } + sCells := row + + switch rt := row.(type) { + case ITableRow: + bg = rt.GetBackgroundColor() + sCells = rt.GetCells() + } + // фон строки for xx := 0; xx < imgWidth; xx++ { for yy := 0; yy < rowHeight; yy++ { @@ -197,7 +206,7 @@ func DrawTableWarm(header []string, rows []any) image.Image { img.Set(xx, y, dividerCol) } - cells := rowToStrings(row) + cells := rowToStrings(sCells) x = 0 for i := 0; i < colCount && i < len(cells); i++ { diff --git a/image_table_test.go b/image_table_test.go index a001da2..0d5894b 100644 --- a/image_table_test.go +++ b/image_table_test.go @@ -1,6 +1,7 @@ package image_table import ( + "image/color" "image/png" "log" "os" @@ -44,7 +45,10 @@ func TestDocument(t *testing.T) { Header: []string{"ID", "Name", "Age"}, Rows: []any{ []any{1, "Иван", 30}, - []any{2, "Пётр", 25}, + &TableBlockStyle{ + Cells: []any{2, "Пётр", 25}, + BackgroundColor: color.RGBA{R: 225, G: 255, B: 225, A: 255}, + }, }, }, { @@ -52,7 +56,10 @@ func TestDocument(t *testing.T) { Header: []string{"Метрика", "Значение"}, Rows: []any{ []any{"Requests", 12000}, - []any{"Errors", 37}, + &TableBlockStyle{ + Cells: []any{"Errors", 37}, + BackgroundColor: color.RGBA{R: 255, G: 225, B: 225, A: 255}, + }, }, }, }, diff --git a/itablerow.go b/itablerow.go new file mode 100644 index 0000000..f31cef1 --- /dev/null +++ b/itablerow.go @@ -0,0 +1,8 @@ +package image_table + +import "image/color" + +type ITableRow interface { + GetCells() []any + GetBackgroundColor() color.RGBA +} diff --git a/table_block.go b/table_block.go new file mode 100644 index 0000000..29c0ea5 --- /dev/null +++ b/table_block.go @@ -0,0 +1,7 @@ +package image_table + +type TableBlock struct { + Title string + Header []string + Rows []any +} diff --git a/tablle_block_style.go b/tablle_block_style.go new file mode 100644 index 0000000..22a2136 --- /dev/null +++ b/tablle_block_style.go @@ -0,0 +1,16 @@ +package image_table + +import "image/color" + +type TableBlockStyle struct { + Cells []any + BackgroundColor color.RGBA +} + +func (s *TableBlockStyle) GetCells() []any { + return s.Cells +} + +func (s *TableBlockStyle) GetBackgroundColor() color.RGBA { + return s.BackgroundColor +}