From c75fea71859277f58a132eb6b90fc24b9b73a108 Mon Sep 17 00:00:00 2001 From: Yongwei Xing Date: Tue, 2 Mar 2021 11:23:29 +0800 Subject: [PATCH] add gridsquare and refactor --- color.go | 539 ++++++++++++++++++++++++++++++++++ example/example.go | 7 +- example/example_gridsquare.go | 16 + example/example_maze.go | 16 +- generativeart.go | 97 +++++- maze.go | 13 +- squaregrid.go | 54 ++++ wave.go | 31 +- 8 files changed, 730 insertions(+), 43 deletions(-) create mode 100644 example/example_gridsquare.go create mode 100644 squaregrid.go diff --git a/color.go b/color.go index bc7c709..a0db994 100644 --- a/color.go +++ b/color.go @@ -22,4 +22,543 @@ var ( Orange = color.RGBA{0xFF, 0xA5, 0x00, 0xFF} Black = color.RGBA{0x00, 0x00, 0x00, 0xFF} White = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF} + Outdoors = []color.RGBA{ + {67, 110, 165, 255}, + {47, 76, 114, 255}, + {165, 138, 3, 255}, + {242, 182, 4, 255}, + {191, 131, 59, 255}, + } + Reddery = []color.RGBA{ + {89, 2, 16, 255}, + {37, 2, 5, 255}, + {186, 7, 55, 255}, + {253, 55, 51, 255}, + {245, 169, 167, 255}, + } + DarkPink = []color.RGBA{ + {2, 64, 89, 255}, + {242, 131, 107, 255}, + {140, 40, 31, 255}, + {191, 69, 57, 255}, + {13, 13, 13, 255}, + } + + Plasma = []color.RGBA{ + color.RGBA{0x0c, 0x07, 0x86, 0xff}, + color.RGBA{0x10, 0x07, 0x87, 0xff}, + color.RGBA{0x13, 0x06, 0x89, 0xff}, + color.RGBA{0x15, 0x06, 0x8a, 0xff}, + color.RGBA{0x18, 0x06, 0x8b, 0xff}, + color.RGBA{0x1b, 0x06, 0x8c, 0xff}, + color.RGBA{0x1d, 0x06, 0x8d, 0xff}, + color.RGBA{0x1f, 0x05, 0x8e, 0xff}, + color.RGBA{0x21, 0x05, 0x8f, 0xff}, + color.RGBA{0x23, 0x05, 0x90, 0xff}, + color.RGBA{0x25, 0x05, 0x91, 0xff}, + color.RGBA{0x27, 0x05, 0x92, 0xff}, + color.RGBA{0x29, 0x05, 0x93, 0xff}, + color.RGBA{0x2b, 0x05, 0x94, 0xff}, + color.RGBA{0x2d, 0x04, 0x94, 0xff}, + color.RGBA{0x2f, 0x04, 0x95, 0xff}, + color.RGBA{0x31, 0x04, 0x96, 0xff}, + color.RGBA{0x33, 0x04, 0x97, 0xff}, + color.RGBA{0x34, 0x04, 0x98, 0xff}, + color.RGBA{0x36, 0x04, 0x98, 0xff}, + color.RGBA{0x38, 0x04, 0x99, 0xff}, + color.RGBA{0x3a, 0x04, 0x9a, 0xff}, + color.RGBA{0x3b, 0x03, 0x9a, 0xff}, + color.RGBA{0x3d, 0x03, 0x9b, 0xff}, + color.RGBA{0x3f, 0x03, 0x9c, 0xff}, + color.RGBA{0x40, 0x03, 0x9c, 0xff}, + color.RGBA{0x42, 0x03, 0x9d, 0xff}, + color.RGBA{0x44, 0x03, 0x9e, 0xff}, + color.RGBA{0x45, 0x03, 0x9e, 0xff}, + color.RGBA{0x47, 0x02, 0x9f, 0xff}, + color.RGBA{0x49, 0x02, 0x9f, 0xff}, + color.RGBA{0x4a, 0x02, 0xa0, 0xff}, + color.RGBA{0x4c, 0x02, 0xa1, 0xff}, + color.RGBA{0x4e, 0x02, 0xa1, 0xff}, + color.RGBA{0x4f, 0x02, 0xa2, 0xff}, + color.RGBA{0x51, 0x01, 0xa2, 0xff}, + color.RGBA{0x52, 0x01, 0xa3, 0xff}, + color.RGBA{0x54, 0x01, 0xa3, 0xff}, + color.RGBA{0x56, 0x01, 0xa3, 0xff}, + color.RGBA{0x57, 0x01, 0xa4, 0xff}, + color.RGBA{0x59, 0x01, 0xa4, 0xff}, + color.RGBA{0x5a, 0x00, 0xa5, 0xff}, + color.RGBA{0x5c, 0x00, 0xa5, 0xff}, + color.RGBA{0x5e, 0x00, 0xa5, 0xff}, + color.RGBA{0x5f, 0x00, 0xa6, 0xff}, + color.RGBA{0x61, 0x00, 0xa6, 0xff}, + color.RGBA{0x62, 0x00, 0xa6, 0xff}, + color.RGBA{0x64, 0x00, 0xa7, 0xff}, + color.RGBA{0x65, 0x00, 0xa7, 0xff}, + color.RGBA{0x67, 0x00, 0xa7, 0xff}, + color.RGBA{0x68, 0x00, 0xa7, 0xff}, + color.RGBA{0x6a, 0x00, 0xa7, 0xff}, + color.RGBA{0x6c, 0x00, 0xa8, 0xff}, + color.RGBA{0x6d, 0x00, 0xa8, 0xff}, + color.RGBA{0x6f, 0x00, 0xa8, 0xff}, + color.RGBA{0x70, 0x00, 0xa8, 0xff}, + color.RGBA{0x72, 0x00, 0xa8, 0xff}, + color.RGBA{0x73, 0x00, 0xa8, 0xff}, + color.RGBA{0x75, 0x00, 0xa8, 0xff}, + color.RGBA{0x76, 0x01, 0xa8, 0xff}, + color.RGBA{0x78, 0x01, 0xa8, 0xff}, + color.RGBA{0x79, 0x01, 0xa8, 0xff}, + color.RGBA{0x7b, 0x02, 0xa8, 0xff}, + color.RGBA{0x7c, 0x02, 0xa7, 0xff}, + color.RGBA{0x7e, 0x03, 0xa7, 0xff}, + color.RGBA{0x7f, 0x03, 0xa7, 0xff}, + color.RGBA{0x81, 0x04, 0xa7, 0xff}, + color.RGBA{0x82, 0x04, 0xa7, 0xff}, + color.RGBA{0x84, 0x05, 0xa6, 0xff}, + color.RGBA{0x85, 0x06, 0xa6, 0xff}, + color.RGBA{0x86, 0x07, 0xa6, 0xff}, + color.RGBA{0x88, 0x07, 0xa5, 0xff}, + color.RGBA{0x89, 0x08, 0xa5, 0xff}, + color.RGBA{0x8b, 0x09, 0xa4, 0xff}, + color.RGBA{0x8c, 0x0a, 0xa4, 0xff}, + color.RGBA{0x8e, 0x0c, 0xa4, 0xff}, + color.RGBA{0x8f, 0x0d, 0xa3, 0xff}, + color.RGBA{0x90, 0x0e, 0xa3, 0xff}, + color.RGBA{0x92, 0x0f, 0xa2, 0xff}, + color.RGBA{0x93, 0x10, 0xa1, 0xff}, + color.RGBA{0x95, 0x11, 0xa1, 0xff}, + color.RGBA{0x96, 0x12, 0xa0, 0xff}, + color.RGBA{0x97, 0x13, 0xa0, 0xff}, + color.RGBA{0x99, 0x14, 0x9f, 0xff}, + color.RGBA{0x9a, 0x15, 0x9e, 0xff}, + color.RGBA{0x9b, 0x17, 0x9e, 0xff}, + color.RGBA{0x9d, 0x18, 0x9d, 0xff}, + color.RGBA{0x9e, 0x19, 0x9c, 0xff}, + color.RGBA{0x9f, 0x1a, 0x9b, 0xff}, + color.RGBA{0xa0, 0x1b, 0x9b, 0xff}, + color.RGBA{0xa2, 0x1c, 0x9a, 0xff}, + color.RGBA{0xa3, 0x1d, 0x99, 0xff}, + color.RGBA{0xa4, 0x1e, 0x98, 0xff}, + color.RGBA{0xa5, 0x1f, 0x97, 0xff}, + color.RGBA{0xa7, 0x21, 0x97, 0xff}, + color.RGBA{0xa8, 0x22, 0x96, 0xff}, + color.RGBA{0xa9, 0x23, 0x95, 0xff}, + color.RGBA{0xaa, 0x24, 0x94, 0xff}, + color.RGBA{0xac, 0x25, 0x93, 0xff}, + color.RGBA{0xad, 0x26, 0x92, 0xff}, + color.RGBA{0xae, 0x27, 0x91, 0xff}, + color.RGBA{0xaf, 0x28, 0x90, 0xff}, + color.RGBA{0xb0, 0x2a, 0x8f, 0xff}, + color.RGBA{0xb1, 0x2b, 0x8f, 0xff}, + color.RGBA{0xb2, 0x2c, 0x8e, 0xff}, + color.RGBA{0xb4, 0x2d, 0x8d, 0xff}, + color.RGBA{0xb5, 0x2e, 0x8c, 0xff}, + color.RGBA{0xb6, 0x2f, 0x8b, 0xff}, + color.RGBA{0xb7, 0x30, 0x8a, 0xff}, + color.RGBA{0xb8, 0x32, 0x89, 0xff}, + color.RGBA{0xb9, 0x33, 0x88, 0xff}, + color.RGBA{0xba, 0x34, 0x87, 0xff}, + color.RGBA{0xbb, 0x35, 0x86, 0xff}, + color.RGBA{0xbc, 0x36, 0x85, 0xff}, + color.RGBA{0xbd, 0x37, 0x84, 0xff}, + color.RGBA{0xbe, 0x38, 0x83, 0xff}, + color.RGBA{0xbf, 0x39, 0x82, 0xff}, + color.RGBA{0xc0, 0x3b, 0x81, 0xff}, + color.RGBA{0xc1, 0x3c, 0x80, 0xff}, + color.RGBA{0xc2, 0x3d, 0x80, 0xff}, + color.RGBA{0xc3, 0x3e, 0x7f, 0xff}, + color.RGBA{0xc4, 0x3f, 0x7e, 0xff}, + color.RGBA{0xc5, 0x40, 0x7d, 0xff}, + color.RGBA{0xc6, 0x41, 0x7c, 0xff}, + color.RGBA{0xc7, 0x42, 0x7b, 0xff}, + color.RGBA{0xc8, 0x44, 0x7a, 0xff}, + color.RGBA{0xc9, 0x45, 0x79, 0xff}, + color.RGBA{0xca, 0x46, 0x78, 0xff}, + color.RGBA{0xcb, 0x47, 0x77, 0xff}, + color.RGBA{0xcc, 0x48, 0x76, 0xff}, + color.RGBA{0xcd, 0x49, 0x75, 0xff}, + color.RGBA{0xce, 0x4a, 0x75, 0xff}, + color.RGBA{0xcf, 0x4b, 0x74, 0xff}, + color.RGBA{0xd0, 0x4d, 0x73, 0xff}, + color.RGBA{0xd1, 0x4e, 0x72, 0xff}, + color.RGBA{0xd1, 0x4f, 0x71, 0xff}, + color.RGBA{0xd2, 0x50, 0x70, 0xff}, + color.RGBA{0xd3, 0x51, 0x6f, 0xff}, + color.RGBA{0xd4, 0x52, 0x6e, 0xff}, + color.RGBA{0xd5, 0x53, 0x6d, 0xff}, + color.RGBA{0xd6, 0x55, 0x6d, 0xff}, + color.RGBA{0xd7, 0x56, 0x6c, 0xff}, + color.RGBA{0xd7, 0x57, 0x6b, 0xff}, + color.RGBA{0xd8, 0x58, 0x6a, 0xff}, + color.RGBA{0xd9, 0x59, 0x69, 0xff}, + color.RGBA{0xda, 0x5a, 0x68, 0xff}, + color.RGBA{0xdb, 0x5b, 0x67, 0xff}, + color.RGBA{0xdc, 0x5d, 0x66, 0xff}, + color.RGBA{0xdc, 0x5e, 0x66, 0xff}, + color.RGBA{0xdd, 0x5f, 0x65, 0xff}, + color.RGBA{0xde, 0x60, 0x64, 0xff}, + color.RGBA{0xdf, 0x61, 0x63, 0xff}, + color.RGBA{0xdf, 0x62, 0x62, 0xff}, + color.RGBA{0xe0, 0x64, 0x61, 0xff}, + color.RGBA{0xe1, 0x65, 0x60, 0xff}, + color.RGBA{0xe2, 0x66, 0x60, 0xff}, + color.RGBA{0xe3, 0x67, 0x5f, 0xff}, + color.RGBA{0xe3, 0x68, 0x5e, 0xff}, + color.RGBA{0xe4, 0x6a, 0x5d, 0xff}, + color.RGBA{0xe5, 0x6b, 0x5c, 0xff}, + color.RGBA{0xe5, 0x6c, 0x5b, 0xff}, + color.RGBA{0xe6, 0x6d, 0x5a, 0xff}, + color.RGBA{0xe7, 0x6e, 0x5a, 0xff}, + color.RGBA{0xe8, 0x70, 0x59, 0xff}, + color.RGBA{0xe8, 0x71, 0x58, 0xff}, + color.RGBA{0xe9, 0x72, 0x57, 0xff}, + color.RGBA{0xea, 0x73, 0x56, 0xff}, + color.RGBA{0xea, 0x74, 0x55, 0xff}, + color.RGBA{0xeb, 0x76, 0x54, 0xff}, + color.RGBA{0xec, 0x77, 0x54, 0xff}, + color.RGBA{0xec, 0x78, 0x53, 0xff}, + color.RGBA{0xed, 0x79, 0x52, 0xff}, + color.RGBA{0xed, 0x7b, 0x51, 0xff}, + color.RGBA{0xee, 0x7c, 0x50, 0xff}, + color.RGBA{0xef, 0x7d, 0x4f, 0xff}, + color.RGBA{0xef, 0x7e, 0x4e, 0xff}, + color.RGBA{0xf0, 0x80, 0x4d, 0xff}, + color.RGBA{0xf0, 0x81, 0x4d, 0xff}, + color.RGBA{0xf1, 0x82, 0x4c, 0xff}, + color.RGBA{0xf2, 0x84, 0x4b, 0xff}, + color.RGBA{0xf2, 0x85, 0x4a, 0xff}, + color.RGBA{0xf3, 0x86, 0x49, 0xff}, + color.RGBA{0xf3, 0x87, 0x48, 0xff}, + color.RGBA{0xf4, 0x89, 0x47, 0xff}, + color.RGBA{0xf4, 0x8a, 0x47, 0xff}, + color.RGBA{0xf5, 0x8b, 0x46, 0xff}, + color.RGBA{0xf5, 0x8d, 0x45, 0xff}, + color.RGBA{0xf6, 0x8e, 0x44, 0xff}, + color.RGBA{0xf6, 0x8f, 0x43, 0xff}, + color.RGBA{0xf6, 0x91, 0x42, 0xff}, + color.RGBA{0xf7, 0x92, 0x41, 0xff}, + color.RGBA{0xf7, 0x93, 0x41, 0xff}, + color.RGBA{0xf8, 0x95, 0x40, 0xff}, + color.RGBA{0xf8, 0x96, 0x3f, 0xff}, + color.RGBA{0xf8, 0x98, 0x3e, 0xff}, + color.RGBA{0xf9, 0x99, 0x3d, 0xff}, + color.RGBA{0xf9, 0x9a, 0x3c, 0xff}, + color.RGBA{0xfa, 0x9c, 0x3b, 0xff}, + color.RGBA{0xfa, 0x9d, 0x3a, 0xff}, + color.RGBA{0xfa, 0x9f, 0x3a, 0xff}, + color.RGBA{0xfa, 0xa0, 0x39, 0xff}, + color.RGBA{0xfb, 0xa2, 0x38, 0xff}, + color.RGBA{0xfb, 0xa3, 0x37, 0xff}, + color.RGBA{0xfb, 0xa4, 0x36, 0xff}, + color.RGBA{0xfc, 0xa6, 0x35, 0xff}, + color.RGBA{0xfc, 0xa7, 0x35, 0xff}, + color.RGBA{0xfc, 0xa9, 0x34, 0xff}, + color.RGBA{0xfc, 0xaa, 0x33, 0xff}, + color.RGBA{0xfc, 0xac, 0x32, 0xff}, + color.RGBA{0xfc, 0xad, 0x31, 0xff}, + color.RGBA{0xfd, 0xaf, 0x31, 0xff}, + color.RGBA{0xfd, 0xb0, 0x30, 0xff}, + color.RGBA{0xfd, 0xb2, 0x2f, 0xff}, + color.RGBA{0xfd, 0xb3, 0x2e, 0xff}, + color.RGBA{0xfd, 0xb5, 0x2d, 0xff}, + color.RGBA{0xfd, 0xb6, 0x2d, 0xff}, + color.RGBA{0xfd, 0xb8, 0x2c, 0xff}, + color.RGBA{0xfd, 0xb9, 0x2b, 0xff}, + color.RGBA{0xfd, 0xbb, 0x2b, 0xff}, + color.RGBA{0xfd, 0xbc, 0x2a, 0xff}, + color.RGBA{0xfd, 0xbe, 0x29, 0xff}, + color.RGBA{0xfd, 0xc0, 0x29, 0xff}, + color.RGBA{0xfd, 0xc1, 0x28, 0xff}, + color.RGBA{0xfd, 0xc3, 0x28, 0xff}, + color.RGBA{0xfd, 0xc4, 0x27, 0xff}, + color.RGBA{0xfd, 0xc6, 0x26, 0xff}, + color.RGBA{0xfc, 0xc7, 0x26, 0xff}, + color.RGBA{0xfc, 0xc9, 0x26, 0xff}, + color.RGBA{0xfc, 0xcb, 0x25, 0xff}, + color.RGBA{0xfc, 0xcc, 0x25, 0xff}, + color.RGBA{0xfc, 0xce, 0x25, 0xff}, + color.RGBA{0xfb, 0xd0, 0x24, 0xff}, + color.RGBA{0xfb, 0xd1, 0x24, 0xff}, + color.RGBA{0xfb, 0xd3, 0x24, 0xff}, + color.RGBA{0xfa, 0xd5, 0x24, 0xff}, + color.RGBA{0xfa, 0xd6, 0x24, 0xff}, + color.RGBA{0xfa, 0xd8, 0x24, 0xff}, + color.RGBA{0xf9, 0xd9, 0x24, 0xff}, + color.RGBA{0xf9, 0xdb, 0x24, 0xff}, + color.RGBA{0xf8, 0xdd, 0x24, 0xff}, + color.RGBA{0xf8, 0xdf, 0x24, 0xff}, + color.RGBA{0xf7, 0xe0, 0x24, 0xff}, + color.RGBA{0xf7, 0xe2, 0x25, 0xff}, + color.RGBA{0xf6, 0xe4, 0x25, 0xff}, + color.RGBA{0xf6, 0xe5, 0x25, 0xff}, + color.RGBA{0xf5, 0xe7, 0x26, 0xff}, + color.RGBA{0xf5, 0xe9, 0x26, 0xff}, + color.RGBA{0xf4, 0xea, 0x26, 0xff}, + color.RGBA{0xf3, 0xec, 0x26, 0xff}, + color.RGBA{0xf3, 0xee, 0x26, 0xff}, + color.RGBA{0xf2, 0xf0, 0x26, 0xff}, + color.RGBA{0xf2, 0xf1, 0x26, 0xff}, + color.RGBA{0xf1, 0xf3, 0x26, 0xff}, + color.RGBA{0xf0, 0xf5, 0x25, 0xff}, + color.RGBA{0xf0, 0xf6, 0x23, 0xff}, + color.RGBA{0xef, 0xf8, 0x21, 0xff}, + } + + Viridis = []color.RGBA{ + color.RGBA{0x44, 0x01, 0x54, 0xff}, + color.RGBA{0x44, 0x02, 0x55, 0xff}, + color.RGBA{0x44, 0x03, 0x57, 0xff}, + color.RGBA{0x45, 0x05, 0x58, 0xff}, + color.RGBA{0x45, 0x06, 0x5a, 0xff}, + color.RGBA{0x45, 0x08, 0x5b, 0xff}, + color.RGBA{0x46, 0x09, 0x5c, 0xff}, + color.RGBA{0x46, 0x0b, 0x5e, 0xff}, + color.RGBA{0x46, 0x0c, 0x5f, 0xff}, + color.RGBA{0x46, 0x0e, 0x61, 0xff}, + color.RGBA{0x47, 0x0f, 0x62, 0xff}, + color.RGBA{0x47, 0x11, 0x63, 0xff}, + color.RGBA{0x47, 0x12, 0x65, 0xff}, + color.RGBA{0x47, 0x14, 0x66, 0xff}, + color.RGBA{0x47, 0x15, 0x67, 0xff}, + color.RGBA{0x47, 0x16, 0x69, 0xff}, + color.RGBA{0x47, 0x18, 0x6a, 0xff}, + color.RGBA{0x48, 0x19, 0x6b, 0xff}, + color.RGBA{0x48, 0x1a, 0x6c, 0xff}, + color.RGBA{0x48, 0x1c, 0x6e, 0xff}, + color.RGBA{0x48, 0x1d, 0x6f, 0xff}, + color.RGBA{0x48, 0x1e, 0x70, 0xff}, + color.RGBA{0x48, 0x20, 0x71, 0xff}, + color.RGBA{0x48, 0x21, 0x72, 0xff}, + color.RGBA{0x48, 0x22, 0x73, 0xff}, + color.RGBA{0x48, 0x23, 0x74, 0xff}, + color.RGBA{0x47, 0x25, 0x75, 0xff}, + color.RGBA{0x47, 0x26, 0x76, 0xff}, + color.RGBA{0x47, 0x27, 0x77, 0xff}, + color.RGBA{0x47, 0x28, 0x78, 0xff}, + color.RGBA{0x47, 0x2a, 0x79, 0xff}, + color.RGBA{0x47, 0x2b, 0x7a, 0xff}, + color.RGBA{0x47, 0x2c, 0x7b, 0xff}, + color.RGBA{0x46, 0x2d, 0x7c, 0xff}, + color.RGBA{0x46, 0x2f, 0x7c, 0xff}, + color.RGBA{0x46, 0x30, 0x7d, 0xff}, + color.RGBA{0x46, 0x31, 0x7e, 0xff}, + color.RGBA{0x45, 0x32, 0x7f, 0xff}, + color.RGBA{0x45, 0x34, 0x7f, 0xff}, + color.RGBA{0x45, 0x35, 0x80, 0xff}, + color.RGBA{0x45, 0x36, 0x81, 0xff}, + color.RGBA{0x44, 0x37, 0x81, 0xff}, + color.RGBA{0x44, 0x39, 0x82, 0xff}, + color.RGBA{0x43, 0x3a, 0x83, 0xff}, + color.RGBA{0x43, 0x3b, 0x83, 0xff}, + color.RGBA{0x43, 0x3c, 0x84, 0xff}, + color.RGBA{0x42, 0x3d, 0x84, 0xff}, + color.RGBA{0x42, 0x3e, 0x85, 0xff}, + color.RGBA{0x42, 0x40, 0x85, 0xff}, + color.RGBA{0x41, 0x41, 0x86, 0xff}, + color.RGBA{0x41, 0x42, 0x86, 0xff}, + color.RGBA{0x40, 0x43, 0x87, 0xff}, + color.RGBA{0x40, 0x44, 0x87, 0xff}, + color.RGBA{0x3f, 0x45, 0x87, 0xff}, + color.RGBA{0x3f, 0x47, 0x88, 0xff}, + color.RGBA{0x3e, 0x48, 0x88, 0xff}, + color.RGBA{0x3e, 0x49, 0x89, 0xff}, + color.RGBA{0x3d, 0x4a, 0x89, 0xff}, + color.RGBA{0x3d, 0x4b, 0x89, 0xff}, + color.RGBA{0x3d, 0x4c, 0x89, 0xff}, + color.RGBA{0x3c, 0x4d, 0x8a, 0xff}, + color.RGBA{0x3c, 0x4e, 0x8a, 0xff}, + color.RGBA{0x3b, 0x50, 0x8a, 0xff}, + color.RGBA{0x3b, 0x51, 0x8a, 0xff}, + color.RGBA{0x3a, 0x52, 0x8b, 0xff}, + color.RGBA{0x3a, 0x53, 0x8b, 0xff}, + color.RGBA{0x39, 0x54, 0x8b, 0xff}, + color.RGBA{0x39, 0x55, 0x8b, 0xff}, + color.RGBA{0x38, 0x56, 0x8b, 0xff}, + color.RGBA{0x38, 0x57, 0x8c, 0xff}, + color.RGBA{0x37, 0x58, 0x8c, 0xff}, + color.RGBA{0x37, 0x59, 0x8c, 0xff}, + color.RGBA{0x36, 0x5a, 0x8c, 0xff}, + color.RGBA{0x36, 0x5b, 0x8c, 0xff}, + color.RGBA{0x35, 0x5c, 0x8c, 0xff}, + color.RGBA{0x35, 0x5d, 0x8c, 0xff}, + color.RGBA{0x34, 0x5e, 0x8d, 0xff}, + color.RGBA{0x34, 0x5f, 0x8d, 0xff}, + color.RGBA{0x33, 0x60, 0x8d, 0xff}, + color.RGBA{0x33, 0x61, 0x8d, 0xff}, + color.RGBA{0x32, 0x62, 0x8d, 0xff}, + color.RGBA{0x32, 0x63, 0x8d, 0xff}, + color.RGBA{0x31, 0x64, 0x8d, 0xff}, + color.RGBA{0x31, 0x65, 0x8d, 0xff}, + color.RGBA{0x31, 0x66, 0x8d, 0xff}, + color.RGBA{0x30, 0x67, 0x8d, 0xff}, + color.RGBA{0x30, 0x68, 0x8d, 0xff}, + color.RGBA{0x2f, 0x69, 0x8d, 0xff}, + color.RGBA{0x2f, 0x6a, 0x8d, 0xff}, + color.RGBA{0x2e, 0x6b, 0x8e, 0xff}, + color.RGBA{0x2e, 0x6c, 0x8e, 0xff}, + color.RGBA{0x2e, 0x6d, 0x8e, 0xff}, + color.RGBA{0x2d, 0x6e, 0x8e, 0xff}, + color.RGBA{0x2d, 0x6f, 0x8e, 0xff}, + color.RGBA{0x2c, 0x70, 0x8e, 0xff}, + color.RGBA{0x2c, 0x71, 0x8e, 0xff}, + color.RGBA{0x2c, 0x72, 0x8e, 0xff}, + color.RGBA{0x2b, 0x73, 0x8e, 0xff}, + color.RGBA{0x2b, 0x74, 0x8e, 0xff}, + color.RGBA{0x2a, 0x75, 0x8e, 0xff}, + color.RGBA{0x2a, 0x76, 0x8e, 0xff}, + color.RGBA{0x2a, 0x77, 0x8e, 0xff}, + color.RGBA{0x29, 0x78, 0x8e, 0xff}, + color.RGBA{0x29, 0x79, 0x8e, 0xff}, + color.RGBA{0x28, 0x7a, 0x8e, 0xff}, + color.RGBA{0x28, 0x7a, 0x8e, 0xff}, + color.RGBA{0x28, 0x7b, 0x8e, 0xff}, + color.RGBA{0x27, 0x7c, 0x8e, 0xff}, + color.RGBA{0x27, 0x7d, 0x8e, 0xff}, + color.RGBA{0x27, 0x7e, 0x8e, 0xff}, + color.RGBA{0x26, 0x7f, 0x8e, 0xff}, + color.RGBA{0x26, 0x80, 0x8e, 0xff}, + color.RGBA{0x26, 0x81, 0x8e, 0xff}, + color.RGBA{0x25, 0x82, 0x8e, 0xff}, + color.RGBA{0x25, 0x83, 0x8d, 0xff}, + color.RGBA{0x24, 0x84, 0x8d, 0xff}, + color.RGBA{0x24, 0x85, 0x8d, 0xff}, + color.RGBA{0x24, 0x86, 0x8d, 0xff}, + color.RGBA{0x23, 0x87, 0x8d, 0xff}, + color.RGBA{0x23, 0x88, 0x8d, 0xff}, + color.RGBA{0x23, 0x89, 0x8d, 0xff}, + color.RGBA{0x22, 0x89, 0x8d, 0xff}, + color.RGBA{0x22, 0x8a, 0x8d, 0xff}, + color.RGBA{0x22, 0x8b, 0x8d, 0xff}, + color.RGBA{0x21, 0x8c, 0x8d, 0xff}, + color.RGBA{0x21, 0x8d, 0x8c, 0xff}, + color.RGBA{0x21, 0x8e, 0x8c, 0xff}, + color.RGBA{0x20, 0x8f, 0x8c, 0xff}, + color.RGBA{0x20, 0x90, 0x8c, 0xff}, + color.RGBA{0x20, 0x91, 0x8c, 0xff}, + color.RGBA{0x1f, 0x92, 0x8c, 0xff}, + color.RGBA{0x1f, 0x93, 0x8b, 0xff}, + color.RGBA{0x1f, 0x94, 0x8b, 0xff}, + color.RGBA{0x1f, 0x95, 0x8b, 0xff}, + color.RGBA{0x1f, 0x96, 0x8b, 0xff}, + color.RGBA{0x1e, 0x97, 0x8a, 0xff}, + color.RGBA{0x1e, 0x98, 0x8a, 0xff}, + color.RGBA{0x1e, 0x99, 0x8a, 0xff}, + color.RGBA{0x1e, 0x99, 0x8a, 0xff}, + color.RGBA{0x1e, 0x9a, 0x89, 0xff}, + color.RGBA{0x1e, 0x9b, 0x89, 0xff}, + color.RGBA{0x1e, 0x9c, 0x89, 0xff}, + color.RGBA{0x1e, 0x9d, 0x88, 0xff}, + color.RGBA{0x1e, 0x9e, 0x88, 0xff}, + color.RGBA{0x1e, 0x9f, 0x88, 0xff}, + color.RGBA{0x1e, 0xa0, 0x87, 0xff}, + color.RGBA{0x1f, 0xa1, 0x87, 0xff}, + color.RGBA{0x1f, 0xa2, 0x86, 0xff}, + color.RGBA{0x1f, 0xa3, 0x86, 0xff}, + color.RGBA{0x20, 0xa4, 0x85, 0xff}, + color.RGBA{0x20, 0xa5, 0x85, 0xff}, + color.RGBA{0x21, 0xa6, 0x85, 0xff}, + color.RGBA{0x21, 0xa7, 0x84, 0xff}, + color.RGBA{0x22, 0xa7, 0x84, 0xff}, + color.RGBA{0x23, 0xa8, 0x83, 0xff}, + color.RGBA{0x23, 0xa9, 0x82, 0xff}, + color.RGBA{0x24, 0xaa, 0x82, 0xff}, + color.RGBA{0x25, 0xab, 0x81, 0xff}, + color.RGBA{0x26, 0xac, 0x81, 0xff}, + color.RGBA{0x27, 0xad, 0x80, 0xff}, + color.RGBA{0x28, 0xae, 0x7f, 0xff}, + color.RGBA{0x29, 0xaf, 0x7f, 0xff}, + color.RGBA{0x2a, 0xb0, 0x7e, 0xff}, + color.RGBA{0x2b, 0xb1, 0x7d, 0xff}, + color.RGBA{0x2c, 0xb1, 0x7d, 0xff}, + color.RGBA{0x2e, 0xb2, 0x7c, 0xff}, + color.RGBA{0x2f, 0xb3, 0x7b, 0xff}, + color.RGBA{0x30, 0xb4, 0x7a, 0xff}, + color.RGBA{0x32, 0xb5, 0x7a, 0xff}, + color.RGBA{0x33, 0xb6, 0x79, 0xff}, + color.RGBA{0x35, 0xb7, 0x78, 0xff}, + color.RGBA{0x36, 0xb8, 0x77, 0xff}, + color.RGBA{0x38, 0xb9, 0x76, 0xff}, + color.RGBA{0x39, 0xb9, 0x76, 0xff}, + color.RGBA{0x3b, 0xba, 0x75, 0xff}, + color.RGBA{0x3d, 0xbb, 0x74, 0xff}, + color.RGBA{0x3e, 0xbc, 0x73, 0xff}, + color.RGBA{0x40, 0xbd, 0x72, 0xff}, + color.RGBA{0x42, 0xbe, 0x71, 0xff}, + color.RGBA{0x44, 0xbe, 0x70, 0xff}, + color.RGBA{0x45, 0xbf, 0x6f, 0xff}, + color.RGBA{0x47, 0xc0, 0x6e, 0xff}, + color.RGBA{0x49, 0xc1, 0x6d, 0xff}, + color.RGBA{0x4b, 0xc2, 0x6c, 0xff}, + color.RGBA{0x4d, 0xc2, 0x6b, 0xff}, + color.RGBA{0x4f, 0xc3, 0x69, 0xff}, + color.RGBA{0x51, 0xc4, 0x68, 0xff}, + color.RGBA{0x53, 0xc5, 0x67, 0xff}, + color.RGBA{0x55, 0xc6, 0x66, 0xff}, + color.RGBA{0x57, 0xc6, 0x65, 0xff}, + color.RGBA{0x59, 0xc7, 0x64, 0xff}, + color.RGBA{0x5b, 0xc8, 0x62, 0xff}, + color.RGBA{0x5e, 0xc9, 0x61, 0xff}, + color.RGBA{0x60, 0xc9, 0x60, 0xff}, + color.RGBA{0x62, 0xca, 0x5f, 0xff}, + color.RGBA{0x64, 0xcb, 0x5d, 0xff}, + color.RGBA{0x67, 0xcc, 0x5c, 0xff}, + color.RGBA{0x69, 0xcc, 0x5b, 0xff}, + color.RGBA{0x6b, 0xcd, 0x59, 0xff}, + color.RGBA{0x6d, 0xce, 0x58, 0xff}, + color.RGBA{0x70, 0xce, 0x56, 0xff}, + color.RGBA{0x72, 0xcf, 0x55, 0xff}, + color.RGBA{0x74, 0xd0, 0x54, 0xff}, + color.RGBA{0x77, 0xd0, 0x52, 0xff}, + color.RGBA{0x79, 0xd1, 0x51, 0xff}, + color.RGBA{0x7c, 0xd2, 0x4f, 0xff}, + color.RGBA{0x7e, 0xd2, 0x4e, 0xff}, + color.RGBA{0x81, 0xd3, 0x4c, 0xff}, + color.RGBA{0x83, 0xd3, 0x4b, 0xff}, + color.RGBA{0x86, 0xd4, 0x49, 0xff}, + color.RGBA{0x88, 0xd5, 0x47, 0xff}, + color.RGBA{0x8b, 0xd5, 0x46, 0xff}, + color.RGBA{0x8d, 0xd6, 0x44, 0xff}, + color.RGBA{0x90, 0xd6, 0x43, 0xff}, + color.RGBA{0x92, 0xd7, 0x41, 0xff}, + color.RGBA{0x95, 0xd7, 0x3f, 0xff}, + color.RGBA{0x97, 0xd8, 0x3e, 0xff}, + color.RGBA{0x9a, 0xd8, 0x3c, 0xff}, + color.RGBA{0x9d, 0xd9, 0x3a, 0xff}, + color.RGBA{0x9f, 0xd9, 0x38, 0xff}, + color.RGBA{0xa2, 0xda, 0x37, 0xff}, + color.RGBA{0xa5, 0xda, 0x35, 0xff}, + color.RGBA{0xa7, 0xdb, 0x33, 0xff}, + color.RGBA{0xaa, 0xdb, 0x32, 0xff}, + color.RGBA{0xad, 0xdc, 0x30, 0xff}, + color.RGBA{0xaf, 0xdc, 0x2e, 0xff}, + color.RGBA{0xb2, 0xdd, 0x2c, 0xff}, + color.RGBA{0xb5, 0xdd, 0x2b, 0xff}, + color.RGBA{0xb7, 0xdd, 0x29, 0xff}, + color.RGBA{0xba, 0xde, 0x27, 0xff}, + color.RGBA{0xbd, 0xde, 0x26, 0xff}, + color.RGBA{0xbf, 0xdf, 0x24, 0xff}, + color.RGBA{0xc2, 0xdf, 0x22, 0xff}, + color.RGBA{0xc5, 0xdf, 0x21, 0xff}, + color.RGBA{0xc7, 0xe0, 0x1f, 0xff}, + color.RGBA{0xca, 0xe0, 0x1e, 0xff}, + color.RGBA{0xcd, 0xe0, 0x1d, 0xff}, + color.RGBA{0xcf, 0xe1, 0x1c, 0xff}, + color.RGBA{0xd2, 0xe1, 0x1b, 0xff}, + color.RGBA{0xd4, 0xe1, 0x1a, 0xff}, + color.RGBA{0xd7, 0xe2, 0x19, 0xff}, + color.RGBA{0xda, 0xe2, 0x18, 0xff}, + color.RGBA{0xdc, 0xe2, 0x18, 0xff}, + color.RGBA{0xdf, 0xe3, 0x18, 0xff}, + color.RGBA{0xe1, 0xe3, 0x18, 0xff}, + color.RGBA{0xe4, 0xe3, 0x18, 0xff}, + color.RGBA{0xe7, 0xe4, 0x19, 0xff}, + color.RGBA{0xe9, 0xe4, 0x19, 0xff}, + color.RGBA{0xec, 0xe4, 0x1a, 0xff}, + color.RGBA{0xee, 0xe5, 0x1b, 0xff}, + color.RGBA{0xf1, 0xe5, 0x1c, 0xff}, + color.RGBA{0xf3, 0xe5, 0x1e, 0xff}, + color.RGBA{0xf6, 0xe6, 0x1f, 0xff}, + color.RGBA{0xf8, 0xe6, 0x21, 0xff}, + color.RGBA{0xfa, 0xe6, 0x22, 0xff}, + color.RGBA{0xfd, 0xe7, 0x24, 0xff}, + } ) diff --git a/example/example.go b/example/example.go index 6514e56..48f971d 100644 --- a/example/example.go +++ b/example/example.go @@ -12,8 +12,7 @@ func formula1(x, y float64) (float64, float64) { func main() { c := generativeart.NewCanva(1600, 1600, 2, 2) - c.FillBackgroud(generativeart.White) - g := generativeart.NewWave(formula1) - g.GenerativePolar(c, generativeart.DarkSalmon) - c.ToPng("g1.png") + c.FillBackground() + c.Draw(generativeart.NewWave(formula1)) + c.ToPNG("g1.png") } diff --git a/example/example_gridsquare.go b/example/example_gridsquare.go new file mode 100644 index 0000000..e5d89f9 --- /dev/null +++ b/example/example_gridsquare.go @@ -0,0 +1,16 @@ +package main + +import ( + "generativeart" + "math/rand" + "time" +) + +func main() { + rand.Seed(time.Now().Unix()) + c := generativeart.NewCanva(600, 600, 1, 1) + c.SetBackground(generativeart.Viridis[rand.Intn(255)]) + c.SetColorSchema(generativeart.Viridis) + c.Draw(generativeart.NewGirdSquares()) + c.ToPNG("gsquare.png") +} diff --git a/example/example_maze.go b/example/example_maze.go index 90dc299..7e3f830 100644 --- a/example/example_maze.go +++ b/example/example_maze.go @@ -1,11 +1,17 @@ package main -import "generativeart" +import ( + "generativeart" + "math/rand" + "time" +) func main() { + rand.Seed(time.Now().Unix()) c := generativeart.NewCanva(800, 800, 2, 2) - c.FillBackgroud(generativeart.Azure) - g := generativeart.NewMaze() - g.Generative(c, generativeart.Tomato) - c.ToPng("maze.png") + c.SetBackground(generativeart.Azure) + c.SetForeground(generativeart.Tomato) + c.FillBackground() + c.Draw(generativeart.NewMaze()) + c.ToPNG("maze.png") } diff --git a/generativeart.go b/generativeart.go index b41ef09..8998c19 100644 --- a/generativeart.go +++ b/generativeart.go @@ -17,6 +17,22 @@ type canva struct { height, width int xaixs, yaixs float64 img *image.RGBA + opts Options +} + +type Options struct { + background color.RGBA + foreground color.RGBA + lineColor color.RGBA + lineWidth float64 + colorSchema []color.RGBA + isPolarCoodinate bool + step int + nIters int + alpha int + rectLenSide int + radius float64 + decay float64 } // NewCanva returns a canva. @@ -27,16 +43,87 @@ func NewCanva(h, w int, x, y float64) *canva { xaixs: x, yaixs: y, img: image.NewRGBA(image.Rect(0, 0, h, w)), + // Set some defaults value + opts: Options{ + background: Azure, + foreground: MistyRose, + lineColor: Tomato, + lineWidth: 3, + colorSchema: Plasma, + isPolarCoodinate: false, + step: 24, + nIters: 20, + alpha: 30, + rectLenSide: 10, + radius: 1.0, + decay: 0.2, + }, } } -// FillBackgroud fills the backgroud of the canva. -func (c *canva) FillBackgroud(rgba color.RGBA) { - draw.Draw(c.img, c.img.Bounds(), &image.Uniform{rgba}, image.ZP, draw.Src) +func (c *canva) SetOptions(opts Options) { + c.opts = opts +} + +func (c *canva) SetBackground(rgba color.RGBA) { + c.opts.background = rgba +} + +func (c *canva) SetForeground(rgba color.RGBA) { + c.opts.foreground = rgba +} + +func (c *canva) SetColorSchema(rgbas []color.RGBA) { + c.opts.colorSchema = rgbas +} + +func (c *canva) SetLineColor(rgba color.RGBA) { + c.opts.lineColor = rgba +} + +func (c *canva) SetLineWidth(lw float64) { + c.opts.lineWidth = lw +} + +func (c *canva) SetPolarCoodinate() { + c.opts.isPolarCoodinate = true +} + +func (c *canva) SetStep(step int) { + c.opts.step = step +} + +func (c *canva) SetIterations(nIters int) { + c.opts.nIters = nIters +} + +func (c *canva) SetAlpha(alpha int) { + c.opts.alpha = alpha +} + +func (c *canva) SetRectLenSide(l int) { + c.opts.rectLenSide = l +} + +func (c *canva) SetRadius(r float64) { + c.opts.radius = r +} + +func (c *canva) SetDecay(d float64) { + c.opts.decay = d +} + +func (c *canva) Draw(e Engine) { + e.Generative(c) +} + +// FillBackground fills the background of the canva. +func (c *canva) FillBackground() { + draw.Draw(c.img, c.img.Bounds(), &image.Uniform{c.opts.background}, image.ZP, draw.Src) } // ToPng saves the image to local with PNG format. -func (c *canva) ToPng(fpath string) error { +func (c *canva) ToPNG(fpath string) error { f, err := os.Create(fpath) if err != nil { return err @@ -54,7 +141,7 @@ func (c *canva) ToPng(fpath string) error { } // ToJpeg saves the image to local with Jpeg format. -func (c *canva) ToJpeg(path string) error { +func (c *canva) ToJPEG(path string) error { f, err := os.Create(path) if err != nil { return err diff --git a/maze.go b/maze.go index 02cc643..cc750b3 100644 --- a/maze.go +++ b/maze.go @@ -1,7 +1,6 @@ package generativeart import ( - "image/color" "math/rand" "github.com/fogleman/gg" @@ -15,21 +14,17 @@ func NewMaze() *maze { } // Generative draws a random maze image. -func (m *maze) Generative(c *canva, rgba color.RGBA) { +func (m *maze) Generative(c *canva) { ctex := gg.NewContextForRGBA(c.img) - ctex.SetColor(rgba) - ctex.SetLineWidth(3.0) - step := 20 + ctex.SetColor(c.opts.background) + ctex.SetLineWidth(c.opts.lineWidth) + step := c.opts.step for x := 0; x < c.width; x += step { for y := 0; y < c.height; y += step { if rand.Float32() > 0.5 { ctex.DrawLine(float64(x), float64(y), float64(x+step), float64(y+step)) - - //ctex.Stroke() } else { ctex.DrawLine(float64(x+step), float64(y), float64(x), float64(y+step)) - ctex.SetLineWidth(3.0) - //ctex.Stroke() } ctex.Stroke() } diff --git a/squaregrid.go b/squaregrid.go new file mode 100644 index 0000000..ce14bb8 --- /dev/null +++ b/squaregrid.go @@ -0,0 +1,54 @@ +package generativeart + +import ( + "github.com/fogleman/gg" + "math/rand" +) + +type girdSquares struct{} + +// NewGirdSquares returns a grid squares generator. +func NewGirdSquares() *girdSquares { + return &girdSquares{} +} + +// Generative draws a grid squares image. +func (g *girdSquares) Generative(c *canva) { + ctex := gg.NewContextForRGBA(c.img) + step := c.opts.step + rectSize := c.opts.rectLenSide + + for x := 0; x < c.width; x += step { + for y := 0; y < c.height; y += step { + cl := c.opts.colorSchema[rand.Intn(255)] + + x0 := float64(x) + y0 := float64(y) + s := float64(rectSize) + + theta := rand.Intn(360) + 1 + for i := 0; i < c.opts.nIters; i++ { + ctex.Push() + + ctex.Translate(x0+float64(step/2), y0+float64(step/2)) + ctex.Rotate(gg.Radians(float64(theta * i))) + + ctex.Scale(s, s) + + ctex.LineTo(-0.5, 0.5) + ctex.LineTo(0.5, 0.5) + ctex.LineTo(0.5, -0.5) + ctex.LineTo(-0.5, -0.5) + ctex.LineTo(-0.5, 0.5) + + ctex.SetLineWidth(c.opts.lineWidth) + ctex.SetColor(c.opts.lineColor) + ctex.StrokePreserve() + ctex.SetRGBA255(int(cl.R), int(cl.G), int(cl.B), c.opts.alpha) + ctex.Fill() + ctex.Pop() + s = s - c.opts.decay*float64(rectSize) + } + } + } +} diff --git a/wave.go b/wave.go index 042d1ec..6889963 100644 --- a/wave.go +++ b/wave.go @@ -1,7 +1,6 @@ package generativeart import ( - "image/color" "math" "github.com/fogleman/gg" @@ -18,34 +17,26 @@ func NewWave(fn Formula) *wave { return &wave{fn: fn} } -// GenerativePolar draws the image in polar coordinate system. -func (w *wave) GenerativePolar(c *canva, rgba color.RGBA) { +// Generative draws the image. +func (w *wave) Generative(c *canva) { ctex := gg.NewContextForRGBA(c.img) for x := -math.Pi; x <= math.Pi; x += 0.01 { for y := -math.Pi; y <= math.Pi; y += 0.01 { xi, yi := w.fn(x, y) - i, j := ConvertCartesianToPolarPixel(xi, yi, c.xaixs, c.yaixs, c.height, c.width) - if i < 0 || i > c.width-1 || j < 0 || j > c.height-1 { - continue + var i, j int + if c.opts.isPolarCoodinate { + i, j = ConvertCartesianToPolarPixel(xi, yi, c.xaixs, c.yaixs, c.height, c.width) + } else { + i, j = ConvertCartesianToPixel(xi, yi, c.xaixs, c.yaixs, c.height, c.width) } - ctex.DrawCircle(float64(i), float64(j), 1.0) - ctex.SetColor(rgba) - ctex.Fill() - //c.img.Set(i, j, rgba) - } - } -} -// Generative draws the image in cartesian coordinate system. -func (w *wave) Generative(c *canva, rgba color.RGBA) { - for x := -math.Pi; x <= math.Pi; x += 0.002 { - for y := -math.Pi; y <= math.Pi; y += 0.002 { - xi, yi := w.fn(x, y) - i, j := ConvertCartesianToPixel(xi, yi, c.xaixs, c.yaixs, c.height, c.width) if i < 0 || i > c.width-1 || j < 0 || j > c.height-1 { continue } - c.img.Set(i, j, rgba) + + ctex.DrawCircle(float64(i), float64(j), c.opts.radius) + ctex.SetColor(c.opts.foreground) + ctex.Fill() } } }