diff --git a/captcha.go b/captcha.go index ce7d7c4..c83901f 100644 --- a/captcha.go +++ b/captcha.go @@ -12,6 +12,7 @@ import ( "io" "math" "math/rand" + "strconv" "time" ) @@ -193,3 +194,63 @@ func drawText(text string, img *image.NRGBA, opts *Options) error { return nil } + +func randomInvertColor(base color.Color) color.Color { + baseLightness := getLightness(base) + var value float64 + if baseLightness >= 0.5 { + value = baseLightness - 0.25 - rng.Float64()*0.2 + } else { + value = baseLightness + 0.25 + rng.Float64()*0.2 + } + hue := float64(rng.Intn(361)) / 360 + saturation := 0.6 + rng.Float64()*0.2 + + return hsva{h: hue, s: saturation, v: value, a: uint8(255)} +} + +func getLightness(colour color.Color) float64 { + r, g, b, a := colour.RGBA() + // transparent + if a == 0 { + return 1.0 + } + max := maxColor(r, g, b) + min := minColor(r, g, b) + + l := (float64(max) + float64(min)) / (2 * 255) + + return l +} + +func maxColor(numList ...uint32) (max uint32) { + for _, num := range numList { + colorVal := num & 255 + if colorVal > max { + max = colorVal + } + } + + return max +} + +func minColor(numList ...uint32) (min uint32) { + min = 255 + for _, num := range numList { + colorVal := num & 255 + if colorVal < min { + min = colorVal + } + } + + return min +} + +func randomEquation() (text string, equation string) { + left := 1 + rng.Intn(9) + right := 1 + rng.Intn(9) + text = strconv.Itoa(left + right) + equation = strconv.Itoa(left) + "+" + strconv.Itoa(right) + + return text, equation +} diff --git a/captcha_test.go b/captcha_test.go index 5f7383c..5e4c3fd 100644 --- a/captcha_test.go +++ b/captcha_test.go @@ -5,6 +5,7 @@ import ( "golang.org/x/image/font/gofont/goregular" "image/color" "testing" + "math/rand" ) func TestNewCaptcha(t *testing.T) { @@ -55,3 +56,49 @@ func TestLoadFont(t *testing.T) { t.Fatal("LoadFont incorrectly parse an invalid font") } } + +func TestMaxColor(t *testing.T) { + var result uint32 + result = maxColor() + if result != 0 { + t.Fatalf("Expect max color to be 0, got %v", result) + } + result = maxColor(1) + if result != 1 { + t.Fatalf("Expect max color to be 1, got %v", result) + } + result = maxColor(52428, 65535) + if result != 255 { + t.Fatalf("Expect max color to be 255, got %v", result) + } + var rng = rand.New(rand.NewSource(0)) + for i := 0; i < 10; i++ { + result = maxColor(rng.Uint32(), rng.Uint32(), rng.Uint32()) + if result > 255 { + t.Fatalf("Number out of range: %v", result) + } + } +} + +func TestMinColor(t *testing.T) { + var result uint32 + result = minColor() + if result != 255 { + t.Fatalf("Expect min color to be 255, got %v", result) + } + result = minColor(1) + if result != 1 { + t.Fatalf("Expect min color to be 1, got %v", result) + } + result = minColor(52428, 65535) + if result != 204 { + t.Fatalf("Expect min color to be 1, got %v", result) + } + var rng = rand.New(rand.NewSource(0)) + for i := 0; i < 10; i++ { + result = minColor(rng.Uint32(), rng.Uint32(), rng.Uint32()) + if result > 255 { + t.Fatalf("Number out of range: %v", result) + } + } +}