# Redmine - project management software # Copyright (C) 2006-2013 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class MyController < ApplicationController before_filter :require_login helper :issues helper :users helper :custom_fields BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues, 'issuesreportedbyme' => :label_reported_issues, 'issueswatched' => :label_watched_issues, 'news' => :label_news_latest, 'calendar' => :label_calendar, 'documents' => :label_document_plural, 'timelog' => :label_spent_time }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'], 'right' => ['issuesreportedbyme'] }.freeze def index page render :action => 'page' end # Show user's page def page @user = User.current @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT end # Edit user's account def account @user = User.current @pref = @user.pref if request.post? @user.safe_attributes = params[:user] @user.pref.attributes = params[:pref] if @user.save @user.pref.save @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) set_language_if_valid @user.language flash[:notice] = l(:notice_account_updated) redirect_to my_account_path return end end end # Destroys user's account def destroy @user = User.current unless @user.own_account_deletable? redirect_to my_account_path return end if request.post? && params[:confirm] @user.destroy if @user.destroyed? logout_user flash[:notice] = l(:notice_account_deleted) end redirect_to home_path end end # Manage user's password def password @user = User.current unless @user.change_password_allowed? flash[:error] = l(:notice_can_t_change_password) redirect_to my_account_path return end if request.post? if @user.check_password?(params[:password]) @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] if @user.save flash[:notice] = l(:notice_account_password_updated) redirect_to my_account_path end else flash[:error] = l(:notice_account_wrong_password) end end end # Create a new feeds key def reset_rss_key if request.post? if User.current.rss_token User.current.rss_token.destroy User.current.reload end User.current.rss_key flash[:notice] = l(:notice_feeds_access_key_reseted) end redirect_to my_account_path end # Create a new API key def reset_api_key if request.post? if User.current.api_token User.current.api_token.destroy User.current.reload end User.current.api_key flash[:notice] = l(:notice_api_access_key_reseted) end redirect_to my_account_path end # User's page layout configuration def page_layout @user = User.current @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup @block_options = [] BLOCKS.each do |k, v| unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)} @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize] end end end # Add a block to user's page # The block is added on top of the page # params[:block] : id of the block to add def add_block block = params[:block].to_s.underscore if block.present? && BLOCKS.key?(block) @user = User.current layout = @user.pref[:my_page_layout] || {} # remove if already present in a group %w(top left right).each {|f| (layout[f] ||= []).delete block } # add it on top layout['top'].unshift block @user.pref[:my_page_layout] = layout @user.pref.save end redirect_to my_page_layout_path end # Remove a block to user's page # params[:block] : id of the block to remove def remove_block block = params[:block].to_s.underscore @user = User.current # remove block in all groups layout = @user.pref[:my_page_layout] || {} %w(top left right).each {|f| (layout[f] ||= []).delete block } @user.pref[:my_page_layout] = layout @user.pref.save redirect_to my_page_layout_path end # Change blocks order on user's page # params[:group] : group to order (top, left or right) # params[:list-(top|left|right)] : array of block ids of the group def order_blocks group = params[:group] @user = User.current if group.is_a?(String) group_items = (params["blocks"] || []).collect(&:underscore) group_items.each {|s| s.sub!(/^block_/, '')} if group_items and group_items.is_a? Array layout = @user.pref[:my_page_layout] || {} # remove group blocks if they are presents in other groups %w(top left right).each {|f| layout[f] = (layout[f] || []) - group_items } layout[group] = group_items @user.pref[:my_page_layout] = layout @user.pref.save end end render :nothing => true end end ='n52' href='#n52'>52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
package chroma

import (
	"fmt"
	"math"
	"strconv"
	"strings"
)

// ANSI2RGB maps ANSI colour names, as supported by Chroma, to hex RGB values.
var ANSI2RGB = map[string]string{
	"#ansiblack":     "000000",
	"#ansidarkred":   "7f0000",
	"#ansidarkgreen": "007f00",
	"#ansibrown":     "7f7fe0",
	"#ansidarkblue":  "00007f",
	"#ansipurple":    "7f007f",
	"#ansiteal":      "007f7f",
	"#ansilightgray": "e5e5e5",
	// Normal
	"#ansidarkgray":  "555555",
	"#ansired":       "ff0000",
	"#ansigreen":     "00ff00",
	"#ansiyellow":    "ffff00",
	"#ansiblue":      "0000ff",
	"#ansifuchsia":   "ff00ff",
	"#ansiturquoise": "00ffff",
	"#ansiwhite":     "ffffff",

	// Aliases without the "ansi" prefix, because...why?
	"#black":     "000000",
	"#darkred":   "7f0000",
	"#darkgreen": "007f00",
	"#brown":     "7f7fe0",
	"#darkblue":  "00007f",
	"#purple":    "7f007f",
	"#teal":      "007f7f",
	"#lightgray": "e5e5e5",
	// Normal
	"#darkgray":  "555555",
	"#red":       "ff0000",
	"#green":     "00ff00",
	"#yellow":    "ffff00",
	"#blue":      "0000ff",
	"#fuchsia":   "ff00ff",
	"#turquoise": "00ffff",
	"#white":     "ffffff",
}

// Colour represents an RGB colour.
type Colour int32

// NewColour creates a Colour directly from RGB values.
func NewColour(r, g, b uint8) Colour {
	return ParseColour(fmt.Sprintf("%02x%02x%02x", r, g, b))
}

// Distance between this colour and another.
//
// This uses the approach described here (https://www.compuphase.com/cmetric.htm).
// This is not as accurate as LAB, et. al. but is *vastly* simpler and sufficient for our needs.
func (c Colour) Distance(e2 Colour) float64 {
	ar, ag, ab := int64(c.Red()), int64(c.Green()), int64(c.Blue())
	br, bg, bb := int64(e2.Red()), int64(e2.Green()), int64(e2.Blue())
	rmean := (ar + br) / 2
	r := ar - br
	g := ag - bg
	b := ab - bb
	return math.Sqrt(float64((((512 + rmean) * r * r) >> 8) + 4*g*g + (((767 - rmean) * b * b) >> 8)))
}

// Brighten returns a copy of this colour with its brightness adjusted.
//
// If factor is negative, the colour is darkened.
//
// Uses approach described here (http://www.pvladov.com/2012/09/make-color-lighter-or-darker.html).
func (c Colour) Brighten(factor float64) Colour {
	r := float64(c.Red())
	g := float64(c.Green())
	b := float64(c.Blue())

	if factor < 0 {
		factor++
		r *= factor
		g *= factor
		b *= factor
	} else {
		r = (255-r)*factor + r
		g = (255-g)*factor + g
		b = (255-b)*factor + b
	}
	return NewColour(uint8(r), uint8(g), uint8(b))
}

// BrightenOrDarken brightens a colour if it is < 0.5 brighteness or darkens if > 0.5 brightness.
func (c Colour) BrightenOrDarken(factor float64) Colour {
	if c.Brightness() < 0.5 {
		return c.Brighten(factor)
	}
	return c.Brighten(-factor)
}

// Brightness of the colour (roughly) in the range 0.0 to 1.0
func (c Colour) Brightness() float64 {
	return (float64(c.Red()) + float64(c.Green()) + float64(c.Blue())) / 255.0 / 3.0
}

// ParseColour in the forms #rgb, #rrggbb, #ansi<colour>, or #<colour>.
// Will return an "unset" colour if invalid.
func ParseColour(colour string) Colour {
	colour = normaliseColour(colour)
	n, err := strconv.ParseUint(colour, 16, 32)
	if err != nil {
		return 0
	}
	return Colour(n + 1)
}

// MustParseColour is like ParseColour except it panics if the colour is invalid.
//
// Will panic if colour is in an invalid format.
func MustParseColour(colour string) Colour {
	parsed := ParseColour(colour)
	if !parsed.IsSet() {
		panic(fmt.Errorf("invalid colour %q", colour))
	}
	return parsed
}

// IsSet returns true if the colour is set.
func (c Colour) IsSet() bool { return c != 0 }

func (c Colour) String() string   { return fmt.Sprintf("#%06x", int(c-1)) }
func (c Colour) GoString() string { return fmt.Sprintf("Colour(0x%06x)", int(c-1)) }

// Red component of colour.
func (c Colour) Red() uint8 { return uint8(((c - 1) >> 16) & 0xff) }

// Green component of colour.
func (c Colour) Green() uint8 { return uint8(((c - 1) >> 8) & 0xff) }

// Blue component of colour.
func (c Colour) Blue() uint8 { return uint8((c - 1) & 0xff) }

// Colours is an orderable set of colours.
type Colours []Colour

func (c Colours) Len() int           { return len(c) }
func (c Colours) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c Colours) Less(i, j int) bool { return c[i] < c[j] }

// Convert colours to #rrggbb.
func normaliseColour(colour string) string {
	if ansi, ok := ANSI2RGB[colour]; ok {
		return ansi
	}
	if strings.HasPrefix(colour, "#") {
		colour = colour[1:]
		if len(colour) == 3 {
			return colour[0:1] + colour[0:1] + colour[1:2] + colour[1:2] + colour[2:3] + colour[2:3]
		}
	}
	return colour
}