Generowanie jaśniejszego/ciemniejszego koloru w css przy użyciu javascript
Powiedzmy, że mam #404040 kod koloru. Jak wygenerować nowy kod koloru, który jest jaśniejszy lub ciemniejszy o 20% (lub podany x%)? Potrzebuję tego do wygenerowania koloru najazdu w dynamicznej witrynie (który kolor zmienia się za pomocą motywu). Dlatego nie można użyć innej klasy lub :hover z predefiniowaną klasą.
Thanks
11 answers
var pad = function(num, totalChars) {
var pad = '0';
num = num + '';
while (num.length < totalChars) {
num = pad + num;
}
return num;
};
// Ratio is between 0 and 1
var changeColor = function(color, ratio, darker) {
// Trim trailing/leading whitespace
color = color.replace(/^\s*|\s*$/, '');
// Expand three-digit hex
color = color.replace(
/^#?([a-f0-9])([a-f0-9])([a-f0-9])$/i,
'#$1$1$2$2$3$3'
);
// Calculate ratio
var difference = Math.round(ratio * 256) * (darker ? -1 : 1),
// Determine if input is RGB(A)
rgb = color.match(new RegExp('^rgba?\\(\\s*' +
'(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
'\\s*,\\s*' +
'(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
'\\s*,\\s*' +
'(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
'(?:\\s*,\\s*' +
'(0|1|0?\\.\\d+))?' +
'\\s*\\)$'
, 'i')),
alpha = !!rgb && rgb[4] != null ? rgb[4] : null,
// Convert hex to decimal
decimal = !!rgb? [rgb[1], rgb[2], rgb[3]] : color.replace(
/^#?([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i,
function() {
return parseInt(arguments[1], 16) + ',' +
parseInt(arguments[2], 16) + ',' +
parseInt(arguments[3], 16);
}
).split(/,/),
returnValue;
// Return RGB(A)
return !!rgb ?
'rgb' + (alpha !== null ? 'a' : '') + '(' +
Math[darker ? 'max' : 'min'](
parseInt(decimal[0], 10) + difference, darker ? 0 : 255
) + ', ' +
Math[darker ? 'max' : 'min'](
parseInt(decimal[1], 10) + difference, darker ? 0 : 255
) + ', ' +
Math[darker ? 'max' : 'min'](
parseInt(decimal[2], 10) + difference, darker ? 0 : 255
) +
(alpha !== null ? ', ' + alpha : '') +
')' :
// Return hex
[
'#',
pad(Math[darker ? 'max' : 'min'](
parseInt(decimal[0], 10) + difference, darker ? 0 : 255
).toString(16), 2),
pad(Math[darker ? 'max' : 'min'](
parseInt(decimal[1], 10) + difference, darker ? 0 : 255
).toString(16), 2),
pad(Math[darker ? 'max' : 'min'](
parseInt(decimal[2], 10) + difference, darker ? 0 : 255
).toString(16), 2)
].join('');
};
var lighterColor = function(color, ratio) {
return changeColor(color, ratio, false);
};
var darkerColor = function(color, ratio) {
return changeColor(color, ratio, true);
};
// Use
var darker = darkerColor('rgba(80, 75, 52, .5)', .2);
var lighter = lighterColor('rgba(80, 75, 52, .5)', .2);
Obsługuje teraz wejście RGB( A), a także hex (3 cyfry lub 6).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-02 17:38:51
Możesz zrobić częściowo przezroczysty biały lub czarny PNG i nakładkę (podkład?) to na hover:
div.button {
background-color: #404040;
}
body>div.button:hover {
background-image: url('blackpixel.png');
}
Nie wymaga JS.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-02 12:20:07
function ColorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
var rgb = "#", c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ("00"+c).substr(c.length);
}
return rgb;
}
Znalezione tutaj: http://www.sitepoint.com/javascript-generate-lighter-darker-color/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-26 10:56:24
W zasadzie wystarczy dodać (dla jaśniejszych) lub odjąć (dla ciemniejszych) równe ilości z każdego ze składników R, G, B.
Spójrz na Domino 2.0 , która jest małą biblioteką javascript, która właśnie to robi.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-02 06:24:22
To stare pytanie.. ale oto mój sposób na zrobienie tego za pomocą replace i split.
var CO='' ;
$('#HoverMe').hover(function(){
CO=$(this).css('backgroundColor' );
var CC= $(this).css('backgroundColor').replace('rgb(','').replace(')','').split(',');
var Change=0.2;
var CR=Math.floor((CC[0]*Change)+parseInt(CC[0]));
var CG=Math.floor((CC[1]*Change)+parseInt(CC[1]));
var CB=Math.floor((CC[2]*Change)+parseInt(CC[2]));
$(this).css('background','rgb('+CR+','+CG+','+CB+')');},
function(){
$(this).css('background',CO);
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-08 19:20:29
Właśnie miałem ten sam problem i rozwiązałem go w następujący sposób, może komuś się przyda. Do tego rozwiązania potrzebne są dwa elementy: zewnętrzny określa kolor, wewnętrzny służy do hightlighting. W moim przypadku mam coś takiego:
<div class="button"><a href="#">Hi Color</a></div>
Następnie definiujesz w CSS:
.button {
display: inline-block;
background-color: blue;
}
.button a {
display: inline-block;
}
.button a:hover {
background-color: rgba(255,255,255,0.5);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-21 19:20:50
To jest tylko modyfikacja powiek ' odpowiedz, ponieważ widziałem tę samą funkcję dwa razy
var pad = function(num, totalChars) {
var pad = '0';
num = num + '';
while (num.length < totalChars) {
num = pad + num;
}
return num;
};
// Ratio is between 0 and 1
var changeColor = function(color, ratio, darker) {
var difference = Math.round(ratio * 255) * (darker ? -1 : 1),
minmax = darker ? Math.max : Math.min,
decimal = color.replace(
/^#?([a-z0-9][a-z0-9])([a-z0-9][a-z0-9])([a-z0-9][a-z0-9])/i,
function() {
return parseInt(arguments[1], 16) + ',' +
parseInt(arguments[2], 16) + ',' +
parseInt(arguments[3], 16);
}
).split(/,/);
return [
'#',
pad(minmax(parseInt(decimal[0], 10) + difference, 0).toString(16), 2),
pad(minmax(parseInt(decimal[1], 10) + difference, 0).toString(16), 2),
pad(minmax(parseInt(decimal[2], 10) + difference, 0).toString(16), 2)
].join('');
};
var lighterColor = function(color, ratio) {
return changeColor(color, ratio, false);
};
var darkerColor = function(color, ratio) {
return changeColor(color, ratio, true);
};
// Use
var darker = darkerColor('#404040', .2);
var lighter = lighterColor('#404040', .2);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:46:57
Być może jquery.colorhelpers.JS funkcje skalowania i dodawania pomogą? Staram się znaleźć lepsze przykłady niż znajdują się w linii dla kodu źródłowego flot, jednak.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-21 17:47:34
Prawdopodobnie przegapiłeś http://www.safalra.com/web-design/javascript/colour-handling-and-processing /. Obsługuje kolory HSV I HSL i konwertuje między nimi i między wartościami RGB.
HSV I HSL są znacznie bardziej "przyjaznymi dla człowieka" reprezentacjami kolorów, więc używanie ich przy tworzeniu jaśniejszego koloru, ciemniejszego, mniej lub bardziej intensywnego lub znalezienie koloru o najlepszym kontraście jest niezwykle proste.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-21 01:10:14
Znalazłem sposób po prostu używając CSS, podobny do rozwiązania Wander Nauta, ale bez użycia obrazu. Jeśli możesz zmienić html, po prostu umieść div za obszarem, który chcesz zmienić z oryginalnym zestawem kolorów jako tłem. Następnie możesz ustawić obszar, który Cię interesuje, półprzezroczystym białym lub czarnym tłem, a następnie rozjaśnić lub przyciemnić swój element.
Zgaduję, że ma swoje ograniczenia, ale dla mnie działa świetnie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-14 18:07:37
Jeśli masz na myśli js po stronie serwera, możesz użyć Stylus , który jest PREPROCESOREM CSS z wbudowanymi funkcjami do tworzenia jaśniejszych/ciemniejszych kolorów itp.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-01 12:42:49