Пример #1
0
// RGB2HSB convert RGB color to HSB (HSV)
func RGB2HSB(r, g, b int) (int, int, int) {
	if r+g+b == 0 {
		return 0, 0, 0
	}

	max := mathutil.Max(mathutil.Max(r, g), b)
	min := mathutil.Min(mathutil.Min(r, g), b)

	var (
		h     int
		s, bb float64
	)

	switch max {
	case min:
		h = 0
	case r:
		h = (60*(g-b)/(max-min) + 360) % 360
	case g:
		h = (60*(b-r)/(max-min) + 120)
	case b:
		h = (60*(r-g)/(max-min) + 240)
	}

	bb = math.Ceil((float64(max) / 255.0) * 100.0)

	if max != 0 {
		fmax, fmin := float64(max), float64(min)
		s = math.Ceil(((fmax - fmin) / fmax) * 100.0)
	}

	return h, int(s), int(bb)
}
Пример #2
0
// Damerau–Levenshtein distance algorithm and code
func getDLDistance(source, target string) int {
	sl := len(source)
	tl := len(target)

	if sl == 0 {
		if tl == 0 {
			return 0
		}
		return tl
	} else if tl == 0 {
		return sl
	}

	h := make([][]int, sl+2)

	for i := range h {
		h[i] = make([]int, tl+2)
	}

	ll := sl + tl

	h[0][0] = ll

	for i := 0; i <= sl; i++ {
		h[i+1][0] = ll
		h[i+1][1] = i
	}

	for j := 0; j <= tl; j++ {
		h[0][j+1] = ll
		h[1][j+1] = j
	}

	sd := make(map[rune]int)

	for _, rn := range source + target {
		sd[rn] = 0
	}

	for i := 1; i <= sl; i++ {
		d := 0

		for j := 1; j <= tl; j++ {
			i1 := sd[rune(target[j-1])]
			j1 := d

			if source[i-1] == target[j-1] {
				h[i+1][j+1] = h[i][j]
				d = j
			} else {
				h[i+1][j+1] = mathutil.Min(h[i][j], mathutil.Min(h[i+1][j], h[i][j+1])) + 1
			}

			h[i+1][j+1] = mathutil.Min(h[i+1][j+1], h[i1][j1]+(i-i1-1)+1+(j-j1-1))
		}

		sd[rune(source[i-1])] = i
	}

	return h[sl+1][tl+1]
}